I'm trying to output some text from grep and highlight in different colors. This is my data:
- name: the name
displayName: "[JIRA REF] the name"
description: JIRA REF
exemptionCategory: waiver
expiresOn: 2024-03-02T00:00:00Z
policyDefinitionReferenceIds:
- REFid
scope: null
I need each key (name, displayName etc) in blue and JIRA REF in red
I've managed to isolate it so the key is one color, but when I add in a secondary condition it only matches that eg
grep -A3 -B4 -P 2024-03-02 --color=auto file.txt \
| grep --color=auto -P \
'expiresOn|displayName|name|exemptionCategory|policyDefinitionReferenceIds|scope|description' \
-A3 -B4
will output each key in red, but when I add in
grep -A3 -B4 -P 2024-03-02 --color=auto file.txt \
| grep --color=auto -P \
'expiresOn|displayName|name|exemptionCategory|policyDefinitionReferenceIds|scope|description' \
-A3 -B4 \
| GREP_COLOR='mt=01;31' grep --color=auto -P '.{0,0}JIRA.{0,4}' \
-A3 -B4
only the JIRA REF is colored

An unoptimized "least effort" solution
While there are better solutions elsewhere on this page, this post (going with my comment to provide actual code) focuses on your current approach (using multiple
greps, sequentially piped into one another) to highlight what went wrong, and how to fix it for a "least effort" solution.Apart from some uniform formatting to improve readability, the following changes were applied:
GREP_COLORSis spelled with anSat the end34for blue, and leaving31for red--color=autoto--color=alwaysas withauto, coloring is only enabled for printing, but disabled when piping into another process. (Thus, the lastgrepdoesn't actually need to be modified for this use-case, but I included it anyway for consistency and potentially needed interoperability.)