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

Let's simplify a bit the regex:

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

The main issue from your regex (apart non needed complexity and readability) is that you use extra erroneous spaces.

No need:

If you think you can have more space than just one in your input, you can use one of:

  • +
  • [[:space:]]+ (POSIX character class)
  • \s+ require -P aka PCRE switch 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