majuss
majuss

Reputation: 21

Replacing pattern with pattern+incrementing number

Here is a snippet of the text files I want to edit:

"> RECORD:W7_S7_L001_R2_001_43549049:wang1141_Nem_0425_6
AGGCTCGAACTTCCTTTGGATGAACTCCAAAAGTCCCTCTAAGAAGTTAACACCTTACAA

"> RECORD:W41_S41_L001_R2_001_36873913:wang1141_Nem_0425_6
TGCCTCAAACTTCCGTGGATTAAACATCCACAGTCCCTCTAAGAAGTTAGCCGTCAACCAA

"> RECORD:W32_S56_L001_R2_001_29095773:wang1141_Nem_0425_6
TGCCTCAAACTTCCTTATGCTTGCACATAAAGTCCCTCTAAGAAGTTAGACTCCCTATTTA

Now I want to replace every ">" with ">$a" and then incrementing "$a++". I found a solution that almost worked:

>awk '/>/{sub(/start/,++n);{print}'

But this only replaces the > with a number without the >.

I appreciate every help!

Upvotes: 0

Views: 29

Answers (1)

Ed Morton
Ed Morton

Reputation: 204721

It's hard to believe that with just a glance at a man page and maybe a quick google you couldn't figure out how to fix the code snippet you had found to do what you want but here it is:

$ awk '/>/{sub(/>/,">"++n)}{print}' file
">1 RECORD:W7_S7_L001_R2_001_43549049:wang1141_Nem_0425_6
AGGCTCGAACTTCCTTTGGATGAACTCCAAAAGTCCCTCTAAGAAGTTAACACCTTACAA

">2 RECORD:W41_S41_L001_R2_001_36873913:wang1141_Nem_0425_6
TGCCTCAAACTTCCGTGGATTAAACATCCACAGTCCCTCTAAGAAGTTAGCCGTCAACCAA

">3 RECORD:W32_S56_L001_R2_001_29095773:wang1141_Nem_0425_6
TGCCTCAAACTTCCTTATGCTTGCACATAAAGTCCCTCTAAGAAGTTAGACTCCCTATTTA

Get the book Effective Awk Programming, 4th Edition, by Arnold Robbins.

Upvotes: 2

Related Questions