我有一个文件,里面的内容是这样的
1234 Name Surname Address
我应该有这样的输出文件
(123) Name Surname Address
我正在使用 Linux sed
命令
sed -e 's/^\([0-9]\{3\}\)\./&/g' file_name
但是我的命令根本没有改变输入文件。
请帮忙,
最佳答案
你不需要两个捕获组,一个就够了,也不需要捕获数字后面的剩余字符,
$ sed 's/\([0-9]\{3\}\)[0-9]/(\1)/g' file
(123) Name Surname Adress
通过 GNU sed,
$ sed -r 's/([0-9]{3})[0-9]/(\1)/g' file
(123) Name Surname Adress
解释:
([0-9]{3}) # Captures first three numbers.
[0-9] # Matches the last number.
(\1) # In the replacement part, it replaces the four numbers by `(captured group)`
关于regex - 我在 Linux 中使用带有正则表达式的 sed 但没有得到预期的答案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24339369/