Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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.

share|improve this question
add comment

1 Answer

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
add comment

Your Answer

 
discard

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.