Linux sed 完全匹配

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16196185/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 22:47:17  来源:igfitidea点击:

Sed match exact

linuxbashsed

提问by Numpty

I'm currently using 's/string/new_string/g'to go through some CSV files, however I'm running into difficulty in the following scenario:

我目前正在使用's/string/new_string/g'一些 CSV 文件,但是在以下情况下我遇到了困难:

I want to replace instances of '192.168.0.11', however it's also catching instances of '192.168.0.111'.

我想替换 的实例'192.168.0.11',但是它也在捕获'192.168.0.111'.

How can I ensure sed only grabs '192.168.0.11'and not '192.168.0.111'? I'll obviously have to repeat this for many variables, so something easily scalable would be appreciated.

我怎样才能确保 sed 只抓取'192.168.0.11'而不是抓取'192.168.0.111'?显然,我必须对许多变量重复这一点,因此很容易扩展的东西将不胜感激。

Thanks!

谢谢!

采纳答案by dave

I'm not sure what the regex you are using is, but if you want an exact match of '192.168.0.11' you can use: s/\<192\.168\.0\.11\>//g

我不确定您使用的正则表达式是什么,但如果您想要完全匹配的 '192.168.0.11',您可以使用: s/\<192\.168\.0\.11\>//g

The \<\>force an exact match.

\<\>部队的精确匹配。