使用 sed 正则表达式分组替换字符

发布于 2024-10-22 11:41:28 字数 524 浏览 1 评论 0原文

我有一个像这样的文本文件:

FOO BAR PIPPO PLUTO 31337 1010
FOOZ BAZ 130
VERY LONG LINE LIKE THIS THEN A NUMBER LIKE 42

我需要将其转换为:

FOO-BAR-PIPPO-PLUTO 31337 1010
FOOZ-BAZ 130
VERY-LONG-LINE-LIKE-THIS-THEN-A-NUMBER-LIKE 42

我能做的最好的事情是:

sed -re 's/([A-Z]+)( )([A-Z]+)/\1-\3/g'

但输出是

FOO-BAR PIPPO-PLUTO 31337 1010
FOOZ-BAZ 130
VERY-LONG LINE-LIKE THIS-THEN A-NUMBER LIKE 42

关闭,但没有雪茄。知道为什么我的正则表达式不起作用吗?

I have a text file that is like this:

FOO BAR PIPPO PLUTO 31337 1010
FOOZ BAZ 130
VERY LONG LINE LIKE THIS THEN A NUMBER LIKE 42

I need to turn it into:

FOO-BAR-PIPPO-PLUTO 31337 1010
FOOZ-BAZ 130
VERY-LONG-LINE-LIKE-THIS-THEN-A-NUMBER-LIKE 42

The best I could do is:

sed -re 's/([A-Z]+)( )([A-Z]+)/\1-\3/g'

but the output is

FOO-BAR PIPPO-PLUTO 31337 1010
FOOZ-BAZ 130
VERY-LONG LINE-LIKE THIS-THEN A-NUMBER LIKE 42

Close, but no cigar. Any idea on why my regex doesn't work?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

木格 2024-10-29 11:41:28

你不能有重叠的匹配。未检测到“BAR PIPPO”,因为在匹配“FOO BAR”时“BAR”已被消耗。

FOO BAR PIPPO PLUTO 31337 1010
------- ===========
   1         2

请尝试以下操作:

$ sed -re 's/ ([A-Z])/-\1/g'

请注意,这没有重叠的匹配项:

FOO BAR PIPPO PLUTO 31337 1010
   --  ==    --
   1   2     3

You can't have overlapping matches. "BAR PIPPO" isn't detected because "BAR" was already consumed when matching "FOO BAR".

FOO BAR PIPPO PLUTO 31337 1010
------- ===========
   1         2

Try this instead:

$ sed -re 's/ ([A-Z])/-\1/g'

Note that this doesn't have overlapping matches:

FOO BAR PIPPO PLUTO 31337 1010
   --  ==    --
   1   2     3
久光 2024-10-29 11:41:28
sed 's/ \([^0-9]\)/-\1/g'

只需查找后跟非数字的空格并将该空格替换为-。这样做的优点是它适用于包含非字母数字字符的行。

概念验证

$ cat ./infile
FOO BAR PIPPO PLUTO 31337 1010
FOOZ BAZ 130
VERY LONG LINE LIKE THIS THEN A NUMBER LIKE 42
THIS LINE HAS $ODD$ #CHARS# IN %IT% 42

$ sed 's/ \([^0-9]\)/-\1/g' ./infile
FOO-BAR-PIPPO-PLUTO 31337 1010
FOOZ-BAZ 130
VERY-LONG-LINE-LIKE-THIS-THEN-A-NUMBER-LIKE 42
THIS-LINE-HAS-$ODD$-#CHARS#-IN-%IT% 42
sed 's/ \([^0-9]\)/-\1/g'

Just look for space followed by not a number and replace that space with a -. The advantage of this is that it will work for lines that have non-alphanumeric characters.

Proof of Concept

$ cat ./infile
FOO BAR PIPPO PLUTO 31337 1010
FOOZ BAZ 130
VERY LONG LINE LIKE THIS THEN A NUMBER LIKE 42
THIS LINE HAS $ODD$ #CHARS# IN %IT% 42

$ sed 's/ \([^0-9]\)/-\1/g' ./infile
FOO-BAR-PIPPO-PLUTO 31337 1010
FOOZ-BAZ 130
VERY-LONG-LINE-LIKE-THIS-THEN-A-NUMBER-LIKE 42
THIS-LINE-HAS-$ODD$-#CHARS#-IN-%IT% 42
辞慾 2024-10-29 11:41:28

非常接近。不过,您不需要匹配多个字母 - 您只需要字母空格字母:(

sed -Ee 's/([A-Z])( )([A-Z])/\1-\3/g' foo.txt 
FOO-BAR-PIPPO-PLUTO 31337 1010
FOOZ-BAZ 130
VERY-LONG-LINE-LIKE-THIS-THEN-A NUMBER-LIKE 42

针对 BSD sed 调整 sed 参数)

Very close. You don't need to match more than one letter though - you just want letter space letter:

sed -Ee 's/([A-Z])( )([A-Z])/\1-\3/g' foo.txt 
FOO-BAR-PIPPO-PLUTO 31337 1010
FOOZ-BAZ 130
VERY-LONG-LINE-LIKE-THIS-THEN-A NUMBER-LIKE 42

(sed params adjusted for BSD sed)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文