Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. 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 have a situation where i want to replace a particular string in many files

Replace a string AAA with another string BBB but there are lot of strings starting with AAA or ending in AAA ,and i want to replace only one on line 34 and keep others intact.

Is it possible to specify by line number,on all files this string is exactly on 34th line.

share|improve this question

migrated from serverfault.com Apr 2 '13 at 13:49

This question came from our site for system and network administrators.

up vote 25 down vote accepted

You can specify line number in sed or NR (number of record) in awk.

awk 'NR==34 { sub("AAA", "BBB") }'

or use FNR (file number record) if you want to specify more than one file on the command line.

awk 'FNR==34 { sub("AAA", "BBB") }'


sed '34s/AAA/BBB/'
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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