我有这样的文本文件:
AAAAAA this is some content.
This is AAAAAA some more content AAAAAA. AAAAAA
This is yet AAAAAA some more [AAAAAA] content.
Run Code Online (Sandbox Code Playgroud)
我需要AAAAAA用递增的数字替换所有出现的,例如,输出看起来像这样:
1 this is some content.
This is 2 some more content 3. 4
This is yet 5 some more [6] content.
Run Code Online (Sandbox Code Playgroud)
如何用递增的数字替换所有匹配?
jay*_*ngh 16
这是一种方法:
$ awk '{for(x=1;x<=NF;x++)if($x~/AAAAAA/){sub(/AAAAAA/,++i)}}1' file
1 this is some content.
This is 2 some more content 3. 4
This is yet 5 some more [6] content.
Run Code Online (Sandbox Code Playgroud)
Perl 解决方案:
perl -pe 'BEGIN{$A=1;} s/AAAAAA/$A++/ge' test.dat
Run Code Online (Sandbox Code Playgroud)