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?

  • You don't need 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 the supercat package back in 2016 to colourise dpkg -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).
    – cas
    Commented Feb 5, 2018 at 2:46
  • ignore the first part of that comment. I forgot that I set my GREP_COLOR='0;33' (yellow) and that the default is 0;31(red - and IMO red on black is unreadable) . I still think that a specialised, configurable colourising tool like supercat or grc or highlight or chroma 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.
    – cas
    Commented Feb 5, 2018 at 3:04
  • 1
    two good Q&As on colourising output: unix.stackexchange.com/questions/104350/multicolored-grep and unix.stackexchange.com/questions/148/… - the multicolored grep Q has a good perl script by terdon that can easily do exactly what you want.
    – cas
    Commented Feb 5, 2018 at 3:19
  • That should be upvotable cas. Yeah, I know I could squeeze more out of (e)grep itself, but on principal I wanted to get sed to do it. Thanks for 'supercat' etc, I'll check those utilities out. Commented Feb 5, 2018 at 15:44

1 Answer 1


man sed will tell you:

Sed commands can be given with ... one address, in which case the
command will only be executed for input lines which match that address;
          Match lines matching the regular expression regexp.

So you can do:

sed '/Package/ s/replace/stuff/'
  • If you want to restrict it to lines starting with Package, you’d use ^Package
    – Jeff Schaller
    Commented Feb 4, 2018 at 23:29
  • Yeah ... man sed ... if I had a few years ;-) it's so hard to find exactly what you want, better to ask. Works fine Tomas: "$ aptitude -v show '?name(grep)' | egrep --color=always "^|grep" | sed '/^Package: / s/\x1b[[^m]*m//g' | sed 's/^(Package: .*)$/\x1b[33;1m\1\x1b[0m/'" Commented Feb 5, 2018 at 0:10
  • Better: " ... sed -r '/^Package: / s/\x1B[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g' ..." Commented Feb 5, 2018 at 0:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .