简体   繁体   中英

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.

插入符号反转选择组,因此,如果[0-9]为“匹配任何数字”,则[^0-9]为“匹配任何非数字”。

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]

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM