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:
- the use of
cat | grep, it's a useless use of cat - to use a
multiple|regexhere
If you think you can have more space than just one in your input, you can use one of:
+[[:space:]]+(POSIXcharacter class)\s+require-PakaPCREswitch ofgrep
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 |