I know -A -B -C could be used to show context around the grep keyword.
My question is, how to show different context on different keyword?
For example, how do I show -A 5 for cat, -B 4 for dog, and -C 1 for monkey:
egrep -A3 "cat|dog|monkey" <file>
// this just show 3 after lines for each keyword.
i don’t think there’s any way to do it with a single grep call, but you could run it through grep once for each variable and concatenate the output:
now
echo "$var"will produce the same output as you would have gotten from your single command, plus line numbers and context indicators (the:prefix indicates a line that matched the pattern exactly, and the-prefix indicates a line being included because of the-A-Band/or-Coptions).the reason i included the line numbers thus far is to preserve the order of the results you would have seen had you managed to do this in one statement. if you like them, great, but if not, you can use the following line to cut them out:
this passes it through once to cut the exact matching lines’ prefixes, then again to cut the context matches’ prefixes.
pretty? no. but it works.