Regex to highlight percentiles above 85% red in a text file

Viewed 38

If anyone is confident in their regex abilities:

Can you help me color all of the percentiles above 85% red. The format would be xxx% where xxx is 86-100%. I'm looking to use a grep command like:

grep -P --colour=always '(?:^|(?<=[\\, ;\-\+\*\/]))[-+]?[0-9]*[\.eE]?[0-9]+|$'

This one highlights every number red which is not what I'm looking for. Thanks for your help! I'm at a new job and am trying to go above and beyond.

1 Answers

You can use

grep -P '[-+]?\b(?:8[6-9]|9[0-9]|100)%' file

See the PCRE regex demo (-P enables the PCRE regex engine).

Details:

  • [-+]? - an optional - or + char
  • \b - a word boundary (there can be no letter, digit or _ immediately to the left of the current location, since the next char is a digit, i.e. a word char)
  • (?:8[6-9]|9[0-9]|100) - 8 followed with 6, 7, 8 or 9 or 9 followed with any one digit or 100
  • % - a % char.
Related