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

I want to replace either every odd or even occurrence of a pattern. Look at the following example:

$ echo aaaaa | sed -e 's/a/b/' -e 's/a/c/' -e 's/a/b/' -e 's/a/c/' -e 's/a/b/'

Is there some command that can do this more concisely? What I'm actually doing is converting *s into BBCode [i] and [/i] tags, so if there's a markdown-to-BBCode converter out there, I'd like to hear about it too.

share|improve this question
up vote 5 down vote accepted
sed 's|\*\([^*]*\)\*|[i]\1[/i]|g'
share|improve this answer
Aah, for some reason I didn't think of group capturing, thanks a million (well... 25, anyway). – evilsoup May 31 '13 at 22:35

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.