0

I have big file which has following lines

abc_a
some lines
abc_b
some lines
abc_c
some lines
abc_d
some lines
abc_e
some lines
abc_f

I want to add number incremental starting from 1 after every abc lines something like

abc_a1
some lines
abc_b2
some lines
abc_c3
some lines
abc_d4
some lines
abc_e5
some lines
abc_f6

Is it possible with sed or awk?

2

With awk:

awk '/^abc/ { $0 = $0 (++a) } 1' file

With perl:

perl -lpe '/^abc/o and s/$/++$a/e' file

or

perl -lpe '/^abc/o and $_ .= ++$a' file

With vim:

:let a=1
::g/^abc/ s/$/\=a/ | let a+=1

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.