gpt4 book ai didi

bash - Sed 捕获重复数字组

转载 作者:行者123 更新时间:2023-11-29 09:22:31 24 4
gpt4 key购买 nike

我正在尝试使用 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

最佳答案

使用 BRE,避免使用 +,因为它不是 POSIX RE 的一部分。

$ cat file
123123
23231
12323
123231
12345
144144
$
$ sed -n 's/^\([0-9]\{1,\}\)\1$/\1/p' file
123
144

关于bash - Sed 捕获重复数字组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56028688/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com