Skip to main content
5 of 12
added 46 characters in body
Gilles Quénot
  • 36.7k
  • 7
  • 74
  • 97

If you are ok, let's simplify a bit with a unique regex:

grep -E '^P [0-9]+ -?24\b' file2.txt 

No need the use of cat | grep, it's a useless use of cat.

If you think you can have more space than just one in the regex, you can use one of:

  • +
  • [[:space:]]+
  • \s+ require -P aka PCRE swithch of grep

The regular expression matches as follows:

Node Explanation
^ start of the line anchor
P 'P' + space
[0-9]+ any character of: '0' to '9' (1 or more times (matching the most amount possible))
a space
-? '-' (optional (matching the most amount possible))
24 '24'
\b word boundary
Gilles Quénot
  • 36.7k
  • 7
  • 74
  • 97