4

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/'
bcbcb

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.

7
sed 's|\*\([^*]*\)\*|[i]\1[/i]|g'
  • 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 clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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