All:
How do we use sed to filter lines by two criteria?
aptitude -v show '?name(grep)' | egrep --color=always "^|grep" | sed 's/^\(Package: .*\)$/\x1b[33;1m\1\x1b[0m/'
In the above, what I'm trying to do is first use grep to color any found matches of my search string ('grep' in this example), and then color all 'Package: ...' heading lines in aptitude's output yellow. However if any 'Package: ...' line has previously had red color added to it by the egrep ... command, what happens is that the line starts yellow, converts to red for the found 'grep' string, and then reverts to normal color when '\e[0m' is found (as I'd expect).
So, it seems to me that what I have to do is find any 'Package: ...' lines and then, in only those lines, strip out possible color codes before coloring the whole line yellow.
sed "s,\x1B\[[0-9;]*[a-zA-Z],,g"
... will strip out the codes, but I need to only run that on the lines starting with 'Package: ....'. What's the spell? Or, might there be a more elegant way to do this?
sed
for this.aptitude -v show '?name(grep)' | grep -E --color=always "^|grep|^Package:.*
. BTW, if you're using debian (rather than, e.g., ubuntu), I added some rules files for thesupercat
package back in 2016 to colourisedpkg -l
output and package description files. AFAIK, the rules files aren't included in other distros but should work if copied (note: i also fixed a minor bug that limited input lines to 1K, which can be a problem with, e.g., Depends: lines).GREP_COLOR='0;33'
(yellow) and that the default is0;31
(red - and IMO red on black is unreadable) . I still think that a specialised, configurable colourising tool likesupercat
orgrc
orhighlight
orchroma
is the right tool for this job. There's a few good questions on this site about that, I'll try to find some and link here.