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.