我需要像这样replace文件中的单词
text text pc text text text text pc text text text text pc text text
我需要用pc1,pc2 ….等取代pc
text text pc1 text text text text pc2 text text text text pc3 text text
我怎样才能做到这一点?
用Perl:
perl -pe 's/\bpc\b/$& . ++$count/ge'
用awk:
awk -v word=pc '{gsub("\<" word "\>", word (++count)); print}'
如果你知道这个词在每一行,并且总是在第三列:
awk '{ $3 = $3 NR; print }'
这是我的awk版本
awk 'BEGIN {count=1}; {if ($3 ~ /pc/) {sub(/pc/,"pc"(count++));print} else {print} }' inputfile
它只会增加计数器,如果$ 3是PC。