2

I need to replace a word in a file like this

text text pc text text
text text pc text text
text text pc text text

i need to replace pc with pc1, pc2 .... etc

text text pc1 text text
text text pc2 text text
text text pc3 text text

How can i do this in one line?

cosmin
  • 121

2 Answers2

3

With Perl:

perl -pe 's/\bpc\b/$& . ++$count/ge'

With awk:

awk -v word=pc '{gsub("\<" word "\>", word (++count)); print}'

If you know the word is on every line and is always in the 3rd column:

awk '{ $3 = $3 NR; print }'
3

This is my version in awk

awk 'BEGIN {count=1}; {if ($3 ~ /pc/) {sub(/pc/,"pc"(count++));print} else {print} }' inputfile

it only increments the counter if the $3 is pc.

user9517
  • 117,122