irritable_phd_syndrome
irritable_phd_syndrome

Reputation: 5077

Searching file containing dollar sign $ with grep

How do I search a file containing $using grep?

I've tried

grep "\$" file.txt

and it does not recognize the $ character to search for.

Upvotes: 1

Views: 1673

Answers (3)

irritable_phd_syndrome
irritable_phd_syndrome

Reputation: 5077

Actually grep \\$ file.txt works

Upvotes: 0

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626950

As I posted in my comment,

grep "\\\$" file.txt

The first \\ is a literal \ for the regex engine, and the thrid \ is used to escape the dollar symbol.

A rule of thumb with matching a literal dollar symbol is:

  1. Escape up to 3 times
  2. Try doubling $ (i.e. $$)

Some of it should work.

Upvotes: 2

ojblass
ojblass

Reputation: 21620

Please see this answer in another stack exchange forum. Basically you should try grep '\$'.

Upvotes: 2

Related Questions