How to grep a line with a backslash at the end of the line?
9,779
Solution 1
grep '\\$' test_file
works fine for me on Solaris 9 and Ubuntu 12.04.
Single quotes and double quotes differ in which characters are taken literally or used as escape/special characters.
Solution 2
I somehow overcame the problem by using below:
grep -Hn "\\\\$"
But I'm not sure why four back slash would work here. It just worked.
Related videos on Youtube
Author by
Marcus Thornton
Updated on September 18, 2022Comments
-
Marcus Thornton over 1 year
I'm trying to grep a line with a backslash at the end of the line like:
abc\ def ghij ...
I hope it can grep the line "abc\". I tried the command below but they didn't work.
grep -EHn "\\$" test_file grep -PHn "\\$" test_file
How should I solve this problem? I just don't know the logic of escape character in grep. The expression did work in vim.
-
Marcus Thornton over 10 yearsIt doesn't work on mine. GNU grep 2.6.3. Red Hat
-
slhck over 10 yearsDon't use double quotes here. Double quotes do not preserve literal
$
, \, and backticks. In your case, \\ becomes a \, and\$
becomes a$
before being passed togrep
, and only thengrep
looks for\$
. Tryecho "\\\\$"
to see why.