【发布时间】:2019-09-25 11:25:17
【问题描述】:
我正在尝试使用 sed 来捕获类似这些示例的组:
123123 (i would want the first group 123)
144144 (I would want the group 144)
但是 sed 似乎没有意识到 \1 是什么。
有没有办法使用 sed 来做到这一点?之后我想用特定的字符串替换第一组。
([0-9]+)\1
我已经尝试过使用上面的正则表达式,sed 似乎没有意识到我想要做什么。
也试过这个:
~/Desktop$ cat file
123123
23231
12323
123231
12345
144144
~/Desktop$ sed -n 's/.*\b\([[:digit:]]\{1,\}\)\1\b.*/\1/p' file
~/Desktop$
~/Desktop$ sed -n -E 's/([0-9]+)\1/specificstring\1/p' file
specificstring12323
specificstring2323
specificstring12323
specificstring14444
~/Desktop$ sed -nE 's/^([0-9]+)\1([^0-9]|$)/\1/p' file
2323
12323
【问题讨论】:
-
sed到底发出了什么?你究竟是如何调用sed的? -
如果一行中没有组怎么办?请从示例中发布一些示例行。见minimal reproducible example
-
sed 在两个示例中都只发出数字 1,如果一行中没有组,它应该发出整个数字序列,所以如果它的 123456 我希望它发出 123456
-
是的,我安装了 GNU sed,一切正常。