This is a test script hello.
This is hello a test script.
This hello is a test script hello.
This is a test script hello.
hello This is a test script hello.
搜索但无法使用awk或sed命令找到上面的正确解释。 任何指导将受到高度赞赏。
我尝试下面的命令需要对下面的一些修改在每个数字之前打印hello字符串
awk '{for(x=1;x<=NF;x++)if($x~/hello/){sub(/hello/,++i)}}1' file.txt > new.txt
输出:
This is a test script 1.
This is 2 a test script.
This 3 is a test script 4.
This is a test script 5.
6 This is a test script 7.
答案 0 :(得分:3)
perl -pe's/hello\K/++$x/ge' input.txt
这会增加每个hello
的计数,从1
开始。 \K
是positive lookbehind。
答案 1 :(得分:1)
awk
仍然适用于您的请求,修改您的命令,
awk '{for(x=1;x<=NF;x++)if($x~/hello/){sub(/hello/,"&"++i,$x)}}1' file.txt > new.txt
简要说明,
&
中的sub
被替换为实际匹配的文字,即"hello"
sub
添加第三个参数,仅处理$x
答案 2 :(得分:0)
使用GNU awk进行单词边界:
$ awk '{while(sub(/\<hello\>/,"&"c+1))c++;}1' file
This is a test script hello1.
This is hello2 a test script.
This hello3 is a test script hello4.
This is a test script hello5.
hello6 This is a test script hello7.