sandeep7289
sandeep7289

Reputation: 187

How to use grep to get special character

I want to grep \" as following text in file(abc) like:

$egrep -n  "^\"$" abc
"CO_FA_SC_600212","2","\"HSE 48\" 48 CHIVALRY AVE"

But its not appearing how could i use egrep or grep to get the line.

Upvotes: 2

Views: 12863

Answers (2)

Lei Mou
Lei Mou

Reputation: 2582

Here are two alternatives:

  1. grep -n "\\\\\"" filename
  2. grep -n '\\"' filename
  3. For the first one, two consecutive \\ act as a single \, and " was escaped by \, so \\" is passed to grep.
  4. For the second one, \ is taken literally, so \\" is passed to grep
  5. 3 and 4 can be verified by echo "\\\\\"" and echo '\\"'

Upvotes: 1

Sunil Bojanapally
Sunil Bojanapally

Reputation: 12658

grep -F 'special char' filename will search the lines which has special characters.

grep -Fn 'special char' filename gets the line number too.

man grep says, -F, --fixed-strings: Interpret PATTERN as a list of fixed strings, separated by newlines, any of which is to be matched

Upvotes: 1

Related Questions