How do I tell grep after piping from cat to only highlight numerals greater than 720, or perhaps, more than two digtis?
It's a fairly simple question. If you would like to include additionally useful information about using grep in this manner, feel free.
Solution 1:
Assuming that by "highlighting numbers greater than 720" you mean that you want to see the full text, but those numbers highlighted with color in the terminal output. I'll further assume that such numbers should also be highlighted if they are part of a word, or otherwise prefixed or suffixed with anything non-numeric (including negation signs or decimal points). However, leading zeroes will not be part of a match.
That said, you can try the regular expression ^|[1-9]\d{3,}|[89]\d{2}|7[2-9]\d
after enable PCRE-compatible expressions in grep
with the -P
switch. Here's an example output, reading from a file example.txt
, with the highlighted parts in bold:
$ grep -P '^|[1-9]\d{3,}|[89]\d{2}|7[2-9]\d' example.txt
something
foo 123 42 1111 777 719 720 1000000
321 -6666 bar999 8383m x1425y 52411.0 00013
The used regular expression consists of multiple parts that are joined together with a pipe (|
), which means that it matches if any of these parts matches:
-
^
simply matches at the beginning of each line, without actually consuming any character. This is added to also show all other lines without appropriate numbers in the output, but without highlighting any part of those lines. -
[1-9]\d{3,}
matches a single digit in the range 1-9 (no leading zero) followed by at least three or more arbitrary digits (\d
is equivalent to[0-9]
). This rule matches all numbers greater than or equal to 1000. -
[89]\d{2}
matches a number starting with an 8 or 9 followed by exactly two more arbitrary digits. This matches all numbers from 800 to 999. -
7[2-9]\d
matches a number starting with a 7 followed by a single digit in the range 2-9 and another single arbitrary digit. That covers all numbers from 720 to 799.
Solution 2:
I had a similar problem - Checking version string that needed to be "Version 2.32" or later. My grep (embedded BusyBox) doesn't support -P option or {n}, so using basic grep:
grep "Version 2\.3[2-9]\|2\.[4-9][0-9]\|[3-9]\.[0-9][0-9]"