具有更多实例和空间的awk循环
这是我的文字:具有更多实例和空间的awk循环,awk,Awk,这是我的文字: haeasdf apple sdfgsdg asdfa orange sdfg sfgs sdfg s apple sgdfgsg sg sdfg orange sfgsfg sdfg orange sgsfgsdg ... 因此,我使用awk来替换 awk {for(i=1;i<=NF;i++){if($i~/apple/){sub(/apple/,"apple ++count",$i)}}} 1; haeasdf apple1 sdfgsd
haeasdf apple sdfgsdg
asdfa orange sdfg sfgs
sdfg s apple sgdfgsg
sg sdfg orange sfgsfg
sdfg orange sgsfgsdg
...
因此,我使用awk
来替换
awk {for(i=1;i<=NF;i++){if($i~/apple/){sub(/apple/,"apple ++count",$i)}}} 1;
haeasdf apple1 sdfgsdg
asdfa orange sdfg sfgs
sdfg s apple2 sgdfgsg
sg sdfg orange sfgsfg
sdfg orange sgsfgsdg
...
我想把苹果香蕉
换成苹果
。输出如下所示
some words here apple1 some words here
some words here orange some words here
some words here apple2 some words here
some words here orange some words here
some words here orange some words here
...
请您尝试使用GNU
awk
中显示的样本编写并测试以下内容。可以在名为str
的awk
变量中使用逗号分隔所有字符串值,这些值将写入输出,并在输出中计数
awk -v str="apple,orange" '
BEGIN{
num=split(str,arr,",")
for(i=1;i<=num;i++){
numFound[arr[i]]
}
}
{
for(i=1;i<=NF;i++){
if($i in numFound){ $i=$i "" ++count[$i] }
}
}
1' Input_file
awk-v str=“苹果,橙色”
开始{
num=拆分(str,arr,“,”)
对于(i=1;我不确定我是否理解你的问题,
awk'{print$0,++a[$0]}“文件
解决你的问题吗?@Sundeep谢谢,很抱歉我没有正确地提出这个问题。@olo,所以对于oragne
你也想把它变成orange1,oragne2
等等……你能确认一下吗?@ravindersing13是的,我想把orange变成orange1,oragne2…
谢谢你的回答,但是,有语法错误,这对我来说很好,测试也很好,你能再次检查它复制的正确性吗(我刚刚重新运行过),一切都很好,请再次复制它重新运行,然后让我知道,干杯。仍然有错误。甚至把它转换成一行awk-v str=“apple,orange”'BEGIN{num=split(str,arr,“,”)表示(i=1;i@olo,在一行中尝试使用awk-vstr=“apple,orange”'BEGIN{num=split(str,arr,,”);for(i=1;i@olo,请您尝试以下内容,awk-vstr=“apple,orange”'BEGIN{num=split(str,arr,,”);for(i=1;i
awk -v str="apple,orange" '
BEGIN{
num=split(str,arr,",")
for(i=1;i<=num;i++){
numFound[arr[i]]
}
}
{
for(i=1;i<=NF;i++){
if($i in numFound){ $i=$i "" ++count[$i] }
}
}
1' Input_file
awk -v str="apple,orange" '
BEGIN{
num=split(str,arr,",")
for(i=1;i<=num;i++){
numFound[arr[i]]
}
}
{
gsub(/apple banana/,"apple")
for(i=1;i<=NF;i++){
if($i in numFound){ $i=$i "" ++count[$i] }
}
}
1
' Input_file