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?

link|improve this question
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 7 '11 at 18:29
feedback

2 Answers

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 }'
link|improve this answer
feedback

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.

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.