E. Reyes
E. Reyes

Reputation: 29

Using grep to find a pattern beginning in a $

I need to find a pattern that starts with a $ is followed by two numbers, a single character that is not a number, and anything else.

I know how to find a pattern starting in a dollar sign and followed by two numbers but I can't figure out how to check for one character that is not a number.

I also need to count how many lines have this pattern.

I have this so far:

grep -Ec '\$[0-9][0-9].....

I don't know what to do. Can someone please help? Any help would be much appreciated.

Upvotes: 0

Views: 102

Answers (3)

morrcahn
morrcahn

Reputation: 13

I would second @realspirituals answer, and if you need to count how many lines have this pattern, you can count how many lines grep ouputs by piping to wc -l. In order to both show the lines and count them in one fell swoop, pipe the output like so

grep "\$[0-9]{2}[^0-9].*" | tee >(wl -l)

where tee will split the output between wl and STDOUT. {2} will cause the prior [0-9] to match twice.

Upvotes: 0

Srini V
Srini V

Reputation: 11355

You can possibly try this regex \$[0-9][0-9][^0-9].*

\$[0-9][0-9][^0-9].*

  • \$ matches the character $ literally
  • [0-9] match a single character present in the list below. 0-9 a single character in the range between 0 and 9
  • [0-9] match a single character present in the list below. 0-9 a single character in the range between 0 and 9
  • [^0-9] match a single character not present in the list below. 0-9 a single character in the range between 0 and 9
  • .* matches any character (except newline) Quantifier: * Between zero and unlimited times, as many times as possible, giving back as needed [greedy]

Upvotes: 1

Alex Howansky
Alex Howansky

Reputation: 53563

The caret character inverts a selection group, so if [0-9] is "match any digit" then [^0-9] is "match any non-digit".

Upvotes: 1

Related Questions