Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question

closed as off topic by Zoredache, Michael Hampton, Scott Pack, mulaz, Tom O'Connor Oct 5 '12 at 12:41

Questions on Server Fault are expected to relate to server, networking, or related infrastructure administration within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here.If this question can be reworded to fit the rules in the help center, please edit the 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

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

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.

share|improve this answer

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