How do you grep for a string containing a slash?

2020-07-02 11:50发布

How should I grep for a string containing a forward slash like ./.?

标签: grep slash
2条回答
SAY GOODBYE
2楼-- · 2020-07-02 12:30

You'll just need to escape the periods with a backslash. So if I have a file foo.txt with contents:

./.
foo
bar
./.

I can run grep \./\. test.txt, which should just print the two ./. lines.

查看更多
萌系小妹纸
3楼-- · 2020-07-02 12:36

The forward slash is not a special character in grep, but may be in tools like sed, Ruby, or Perl. You probably want to escape your literal periods, though, and it does no harm to escape the slash. This should work in all cases:

\.\/\.
查看更多
登录 后发表回答