awk 'BEGIN{ IGNORECASE=1; m1=0; m2=0 }
{ x=gsub(/te[a-zA-Z]* /,"yyyyy ",$0); m1+=(x!=0); m2+=x; print }
END{ print "The file has " NR " lines and " m1 " out of these were modified, with " m2 " changes"}' inputfile
或
awk 'BEGIN{ IGNORECASE=1; m1=0; m2=0 }
{ x=gsub(/te[[:alhpa:]]* /,"yyyyy ",$0); m1+=(x!=0); m2+=x; print }
END{ print "The file has " NR " lines and " m1 " out of these were modified, with " m2 " changes"}' inputfile
如果您不需要输出更改后的文本,请从第二行中删除 print。
输出:
Hello everyone,
today is a great day to get yyyyy by mr. yyyyy here!
Don't te11 anyone!
The file has 3 lines and 1 out of these were modified, with 2 changes
编辑:由于Teheran! 上的评论,我将输入文件更改为:
Hello everyone,
today is a great day, to get tested by mr. Tenet here!
time to light some external fire in Teheran!
Don't te11 anyone!
脚本:
awk 'BEGIN{ IGNORECASE=1; m1=0; m2=0 }
{ x=gsub(/\<te[[:alpha:]^[0-9][:punct:]]*/,"yyyyy ",$0); m1+=(x!=0); m2+=x; print }
END{ print "The file has " NR " lines and " m1 " out of these were modified, with " m2 " changes"}' inputfile
这似乎工作正常:
Hello everyone,
today is a great day, to get yyyyy by mr. yyyyy here!
time to light some external fire in yyyyy
Don't te11 11 anyone!
The file has 4 lines and 3 out of these were modified, with 4 changes