Mar*_*k T 5 regex awk grep match string-comparison
如何使用awk搜索文件中的完全匹配?
test.txt
hello10
hello100
hello1000
Run Code Online (Sandbox Code Playgroud)
我尝试了以下内容,它返回所有3行
awk '$0 ~ /^hello10/{print;}' test.txt
Run Code Online (Sandbox Code Playgroud)
grep -w hello10可以解决这个问题,但是在这个方面,grep版本非常有限,只有很少的交换机可用
要进行全行正则表达式匹配,您需要使用^和来锚定行的开头和结尾$:
$ awk '/^hello10$/' test.txt
hello10
Run Code Online (Sandbox Code Playgroud)
但是你实际上并没有在我们刚添加的锚点旁边使用任何正则表达式功能,这意味着你实际上想要进行简单的旧字符串比较:
$ awk '$0=="hello10"' test.txt
hello10
Run Code Online (Sandbox Code Playgroud)