Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top
$ echo -e 'CH12\nCH23au' | sed '/^CH/s=^=<b>='

I know I can match lines starting with CH by ^CH but how can I match multiple patterns?






How to only put <b> where there is ^CH and au in the line?

share|improve this question
up vote 6 down vote accepted

If the CH must occur at the beginning of the line, the order of CH and au is fixed, so you can look for ^CH.*au.

$ echo -e 'CH12\nCH23au' | sed '/^CH.*au/s=^=<b>='

If the order of the two patterns is not fixed, one could do something like

sed -e '/pattern1/{;/pattern2/s/old/new/;}'

but the perl solution

perl -pe 'if (/pattern1/ && /pattern2/) {s/old/new/;}'

is probably more readable.

share|improve this answer

Another approach:

sed -e '/^CH/!b' -e '/au/!b' -e 's/^/<b>/'

b (branch to the end if not given any label argument) is like continue or next in other languages. So the above is like:

for (; line = readline(); print line) { # The implicit loop in sed
  if (!/^CH/) continue;
  if (!/au/) continue;
  line =~ s/^/<b>/
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.