Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you grep for a string containing a slash?

Tags:

grep

slash

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

like image 470
LookIntoEast Avatar asked Jun 27 '12 21:06

LookIntoEast


2 Answers

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:

\.\/\.
like image 88
Todd A. Jacobs Avatar answered Oct 18 '22 09:10

Todd A. Jacobs


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.

like image 29
Peter Avatar answered Oct 18 '22 09:10

Peter