views:

238

answers:

3

Given the plain text file with lines

bli foo bla
 abc
 dfg
bli foo bla
 hik
 lmn

what sed or awk magic transforms it to

bli foo_01 bla
 abc
 dfg
bli foo_02 bla
 hik
 lmn

so that every occurence of 'foo' is replaced by 'foo_[occurence number]'.

+1  A: 

This probably isn't what you require, but it might give some ideas in the right direction.

Administrator@snadbox3 ~
$ cd c:/tmp

Administrator@snadbox3 /cygdrive/c/tmp
$ cat <<-eof >foo.txt
> foo
>  abc
>  dfg
> foo
>  hik
>  lmn
> eof

Administrator@snadbox3 /cygdrive/c/tmp
$ awk '/^foo$/{++fooCount; print($0 "_" fooCount);} /^ /{print}' foo.txt
foo_1
 abc
 dfg
foo_2
 hik
 lmn


EDIT:

I'm a day late and a penny short, again ;-(


EDIT2:

Character encodings is another thing to lookout for... Java source code isn't necessarily in the systems default encoding... it's quit UTF-8 encoded, to allow for any embedded "higher order entities" ;-) Many *nix utilities still aren't charset-aware.

corlettk
+1  A: 
awk '!/foo/||sub(/foo/,"&_"++_)' infile

Use gawk, nawk or /usr/xpg4/bin/awk on Solaris.

radoulov
A: 

This is another way to express radoulov's answer

awk '/foo/ {sub(/foo/, "&_" sprintf("%02d",++c))} 1' infile

You should take care that you don't match "foobar" while looking for "foo":

gawk '/\<foo\>/ {sub(/\<foo\>/, "&_" sprintf("%02d",++c))} 1'
glenn jackman