[英]Match and replace digits with sed in specific lines
Using the Ubuntu 12.04 and vim 2.22.0, inside the vim editor I shall use: 在vim编辑器中使用Ubuntu 12.04和vim 2.22.0,我将使用:
:9;13;17;21s/\d\+/1/
to match digits before ^M (and sometime just digits without any tails) and change them to 1. This pattern occurs only in the beginning of the line #9,13,17 and 21. ie: 匹配^ M之前的数字(有时只是没有尾巴的数字)并将其更改为1。此模式仅出现在#9、13、17和21行的开头。即:
vi _ccc_info_datasets:
...
=====
2
../../automotive_susan_data/2.pgm output_large.corners.pgm -c > ftmp_out
691^M // ---------> change to 1^M
=====
...
As I have understood, the same can't go with sed as like : 据我了解,sed不能像这样:
find . type f -name "_ccc_info_datasets" -exec sed -i '9;13;17;21s/\d\+/1/' {} \;
for changing several files in bash. 用于更改bash中的多个文件。 I have tried :
我努力了 :
find . type f -name "_ccc_info_datasets" -exec sed -i '9;13;17;21s/[0-9]{1;\}/1/' {} \;
but it executes with no errors but no results. 但它执行时没有错误,但是没有结果。 Any helps is appreciated ;)
任何帮助表示赞赏;)
You cannot use sed like that. 您不能像这样使用sed。 You need to use multiple sed replacements with
-e
switch like this: 您需要使用
-e
开关使用多个sed替换,如下所示:
sed -i.bak -e '9s/[0-9][0-9]*\('$'\r''\)/1\1/' -e '13s/[0-9][0-9]*\('$'\r''\)/1\1/' \
-e '17s/[0-9][0-9]*\('$'\r''\)/1\1/' -e '21s/[0-9][0-9]*\('$'\r''\)/1\1/' file
Update: Though awk doesn't support inline editing I believe using awk will be much cleaner for this task. 更新:尽管awk不支持内联编辑,但我相信使用awk可以更轻松地完成此任务。 Consider below awk command:
考虑以下awk命令:
awk 'NR ~ /^9|13|17|21$/{sub(/[0-9]+\r$/, "1\r")}1' file > _temp && mv _temp file
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.