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
什么sed或awk魔术将它转化为
bli foo_01 bla
abc
dfg
bli foo_02 bla
hik
lmn
so that every occurence of 'foo' is replaced by 'foo_[occurence number]'.
这样'foo'的每次出现都被'foo_ [出现次数]'取代。
3 个解决方案
#1
This is another way to express radoulov's answer
这是表达radoulov答案的另一种方式
awk '/foo/ {sub(/foo/, "&_" sprintf("%02d",++c))} 1' infile
You should take care that you don't match "foobar" while looking for "foo":
在寻找“foo”时,你应该注意你不匹配“foobar”:
gawk '/\<foo\>/ {sub(/\<foo\>/, "&_" sprintf("%02d",++c))} 1'
#2
awk '!/foo/||sub(/foo/,"&_"++_)' infile
Use gawk, nawk or /usr/xpg4/bin/awk on Solaris.
在Solaris上使用gawk,nawk或/ usr / xpg4 / bin / awk。
#3
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.
字符编码是值得关注的另一件事... Java源代码不一定是系统默认编码...它退出UTF-8编码,允许任何嵌入的“更高阶实体”;-)许多* nix实用程序仍然没有charset意识。
#1
This is another way to express radoulov's answer
这是表达radoulov答案的另一种方式
awk '/foo/ {sub(/foo/, "&_" sprintf("%02d",++c))} 1' infile
You should take care that you don't match "foobar" while looking for "foo":
在寻找“foo”时,你应该注意你不匹配“foobar”:
gawk '/\<foo\>/ {sub(/\<foo\>/, "&_" sprintf("%02d",++c))} 1'
#2
awk '!/foo/||sub(/foo/,"&_"++_)' infile
Use gawk, nawk or /usr/xpg4/bin/awk on Solaris.
在Solaris上使用gawk,nawk或/ usr / xpg4 / bin / awk。
#3
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.
字符编码是值得关注的另一件事... Java源代码不一定是系统默认编码...它退出UTF-8编码,允许任何嵌入的“更高阶实体”;-)许多* nix实用程序仍然没有charset意识。