我有一个看起来像这样的文件listofnames.txt
。
this-is-name-1
this-is-name-2
...
this-is-name-11
this-is-name-12
this-is-name-13
this-is-name-14
...
this-is-name-21
....
我要完全匹配某个单词,并在其旁边添加一个“ 0”。我使用以下命令:
sed -i '/this-is-name-1/s/$/ 0/' ~/listofnames.txt
但是,除了这个,
this-is-name-1 0
this-is-name-2
...
this-is-name-11
this-is-name-12
this-is-name-13
this-is-name-14
...
this-is-name-21
....
我明白了
this-is-name-1 0
this-is-name-2
...
this-is-name-11 0
this-is-name-12 0
this-is-name-13 0
this-is-name-14 0
...
this-is-name-21
....
我的命令出了什么问题?我该如何解决它并做我需要的事情。似乎是在进行部分比赛,而不是全部比赛。
您必须将单词匹配:this-is-name-1
这样才能排除诸如此类的情况this-is-name-11
。执行此操作的方法有很多,具体取决于文件的其余内容以及要保护其免受哪些情况影响。例如,\b
匹配单词赏金,包括空格字符,行的开头/结尾以及其他非字母数字字符。看来对您的情况有好处:
sed -i '/\bthis-is-name-1\b/s/$/ 0/' file
您会发现此阅读非常有用:参考-此正则表达式是什么意思?
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句