Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
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.

share|improve this answer
    
It doesn't work on mine. GNU grep 2.6.3. Red Hat – Marcus Thornton Jan 23 '14 at 6:41

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.

share|improve this answer
1  
Don't use double quotes here. Double quotes do not preserve literal $, \, and backticks. In your case, \\ becomes a \, and \$ becomes a $ before being passed to grep, and only then grep looks for \$. Try echo "\\\\$" to see why. – slhck Jan 23 '14 at 8:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .