我需要用特定格式替换所有出现的字符串(在我的例子中冒号后跟一些数字),并在文件中使用带有后缀的相同字符串,如下所示:
:123456 -> :123456_suffix
有没有办法用sed或其他unix命令行工具来做?
答案 0 :(得分:2)
Sed应该这样做:
sed -i~ -e 's/:\([0-9]\{1,\}\)/:\1_suffix/g' file
^ ^ ^ ^ ^ ^
| | | | | |
start capture | | end | globally, i.e. not just the first
group | | capture | occurrence on a line
any digit | the first capture
one or group contents
more times
如果不支持-i
,只需创建一个新文件并替换旧文件:
sed ... > newfile
mv oldfile oldfile~ # a backup
mv newfile oldfile
答案 1 :(得分:2)
使用sed,
sed 's/\(:[0-9]\+\)/\1_suffix/g' file
添加-i
修饰符,如果您要进行就地编辑。