bashshellgrep

Grep For Patterns But Exclude Results Where the Pattern is Nested


I have large files containing lines with comma separated key=value pairs. For example:

DATE=2024/08/19,TIME=09:15:51,ID=9,PID=49,ERR=12,RCA=52
DATE=2024/08/19,TIME=10:28:40,ID=10,PID=50,ERR=,RCA=52
DATE=2024/08/19,TIME=10:28:42,ID=11,PID=51,ERR=4,RCA=52

I have a grep that extracts the key values I'm interested in:

grep -oP '(DATE|ID|ERR)=[^,]*' files-*.txt

The output I'd like is:

DATE=2024/08/19
ID=9
ERR=12
DATE=2024/08/19
ID=10
ERR=
DATE=2024/08/19
ID=11
ERR=4

But my grep is bringing the PID (minus the P) into the results as well:

DATE=2024/08/19
ID=9
ID=49
ERR=12
DATE=2024/08/19
ID=10
ID=50
ERR=
DATE=2024/08/19
ID=11
ID=51
ERR=4

How can I modify the grep to return the ID key and value, but not the PID key and value?

Any help you can provide would be much appreciated


Solution

  • You should use a word boundary (\b) to avoid matching PID:

    $ grep -oP '(DATE|\bID|ERR)=[^,]*' files-*.txt