How should I grep for a string containing a forward slash like ./.
?
Asked
Active
Viewed 6.1k times
22

TRiG
- 10,148
- 7
- 57
- 107

LookIntoEast
- 8,048
- 18
- 64
- 92
-
9Can you maybe accept an answer to mark the question as resolved? – MERose Nov 05 '15 at 14:30
-
On Windows (Git for Windows, MinGW, etc.), forward slashes are sometimes affected by path conversion. Prefix the grep command with `MSYS_NO_PATHCONV=1` if needed. – Joey Adams Dec 12 '22 at 17:54
3 Answers
15
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:
\.\/\.

Todd A. Jacobs
- 81,402
- 15
- 141
- 199
13
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.

Peter
- 1,349
- 11
- 21