简体   繁体   English

使用grep查找以$开头的模式

[英]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].*

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

  • \\$ matches the character $ literally \\ $从字面上匹配字符$
  • [0-9] match a single character present in the list below. [0-9]匹配以下列表中存在的单个字符。 0-9 a single character in the range between 0 and 9 0-9介于0到9之间的单个字符
  • [0-9] match a single character present in the list below. [0-9]匹配以下列表中存在的单个字符。 0-9 a single character in the range between 0 and 9 0-9介于0到9之间的单个字符
  • [^0-9] match a single character not present in the list below. [^ 0-9]匹配以下列表中不存在的单个字符。 0-9 a single character in the range between 0 and 9 0-9介于0到9之间的单个字符
  • .* matches any character (except newline) Quantifier: * Between zero and unlimited times, as many times as possible, giving back as needed [greedy] 。*匹配任何字符(换行符除外)量词:*在0到无限次之间,尽可能多地重复,并根据需要返回[贪婪]

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 . 我会第二个@realspirituituals回答,如果您需要计算多少行具有此模式,可以通过管道传输到wc -l来计算grep输出的行数。 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 . tee将在wlSTDOUT之间分配STDOUT {2} will cause the prior [0-9] to match twice. {2}将导致前一个[0-9]匹配两次。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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