sed tricks: conditional replacement

2015/04/23

I wanted to change the read names in a fasta file. Basically, I wanted to run a sed replacement, but only on lines that start with >, changing lines like >GoodInfo_LotsOfTrash into just >GoodInfo, but I wanted to leave lines that didn’t start with > alone.

I found a nice answer:

<code>sed '/conditional_pattern/ s/pattern/replacement/g'
</code>

In my case, I wanted to chuck everything after an underscore _, so sed '/^>/ /_.*//'. The command line wins again.