首页 > 解决方案 > grep 不能很好地搜索双引号。甚至没有\"

问题描述

我有一个包含以下行的文件:

s:15:\"https://cnn.com\"

cat myfile.txt | grep https://cnn.com作品。

但我也想找到这s:15部分,所以我尝试:

cat myfile.txt | grep 's:15:\"https://cnn.com\"'

零结果。我究竟做错了什么?

标签: bashgrep

解决方案


您的命令的问题不是引号而是反斜杠。由于原始文件中有反斜杠,因此您也需要将反斜杠与 grep 匹配(因此,将它们转义):

$ cat myfile.txt | grep 's:15:\\"https://cnn.com\\"'
s:15:\"https://cnn.com\"

推荐阅读