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?

glenn jackman
  • 4,320
  • 16
  • 19
cosmin
  • 121
  • 1
  • 2
  • The word isn't allways on 3rd column. Actually i want this for different administration jobs that i perfom daily.Modify conf files etc... And i need to do this quickly.I cand do'it in python or c but ... there isn't allways the time... Tx for the responses. – cosmin Oct 07 '11 at 18:29

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 }'
glenn jackman
  • 4,320
  • 16
  • 19
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
  • 114,104
  • 20
  • 206
  • 289