LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Replacing text on specific lines with sed or awk? (http://www.linuxquestions.org/questions/linux-newbie-8/replacing-text-on-specific-lines-with-sed-or-awk-592276/)

Lantzvillian 10-16-2007 12:08 PM

Replacing text on specific lines with sed or awk?
 
Hey there,
I am looking for the command syntax to do the following.
On lines one and five replace x with y? Now I know sed could do something like that, but how is the question?

thanks,

Ronnie

colucix 10-16-2007 12:17 PM

A method with sed:
Code:

sed 's/x/y/;n;n;n;n;s/x/y/'
n reads next line.

colucix 10-16-2007 12:26 PM

And a method with awk
Code:

gawk 'NR==5||NR==1{sub("x","y")}{print}'

Lantzvillian 10-16-2007 12:41 PM

thanks gordie,
do you apply it to the text file exampletxt? gawk 'NR==5||NR==1{sub("x","y")}{print}' >exampletext

Lantzvillian 10-16-2007 12:54 PM

gotter thanks everyone

juergen 10-17-2007 10:00 AM

Another sed approach:
Code:

sed '1,5s/x/y/'


All times are GMT -5. The time now is 09:22 AM.