我有文件:
# more file
(WORD = (TCPIP = (PROTOCOL = TCP)(WORD = ALIAS_NAME)(PORT = 10234))
但是当我尝试匹配文件中的部分行(通过grep -w)时,grep也匹配该行(尽管这是整行的一部分)
grep -w "(TCPIP = (PROTOCOL = TCP)(WORD = ALIAS_NAME)(PORT = 10234))" file
(WORD = (TCPIP = (PROTOCOL = TCP)(WORD = ALIAS_NAME)(PORT = 10234))
我的问题如何完全匹配文件中的行
所以,如果我使用grep或其他类似sed / awk的东西,它只会匹配整行?
利迪娅
答案 0 :(得分:2)
通过返回整行,grep只是显示已在该行中找到字符串 。如果您只想在命令行上看到匹配的文本,您可以(例如)使用-o(仅匹配):
echo $name | grep -o "(WORD = (TCPIP = (PROTOCOL = TCP)(WORD = ALIAS_NAME)(PORT = 10234))"
只是在CL上看到结果,省略“echo $ name |”。
如果要捕获变量中的确切文本,可以执行以下操作:
name=`grep -o "(WORD = (TCPIP = (PROTOCOL = TCP)(WORD = ALIAS_NAME)(PORT = 10234))" /Users/deveritt/Desktop/test.html`
echo $name
(WORD = (TCPIP = (PROTOCOL = TCP)(WORD = ALIAS_NAME)(PORT = 10234))