LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-16-2007, 11:08 AM   #1
Lantzvillian
Member
 
Registered: Oct 2007
Location: BC, Canada
Distribution: Fedora, Debian
Posts: 210

Rep: Reputation: 41
Question 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
 
Old 10-16-2007, 11:17 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
A method with sed:
Code:
sed 's/x/y/;n;n;n;n;s/x/y/'
n reads next line.
 
Old 10-16-2007, 11:26 AM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
And a method with awk
Code:
gawk 'NR==5||NR==1{sub("x","y")}{print}'
 
Old 10-16-2007, 11:41 AM   #4
Lantzvillian
Member
 
Registered: Oct 2007
Location: BC, Canada
Distribution: Fedora, Debian
Posts: 210

Original Poster
Rep: Reputation: 41
thanks gordie,
do you apply it to the text file exampletxt? gawk 'NR==5||NR==1{sub("x","y")}{print}' >exampletext
 
Old 10-16-2007, 11:54 AM   #5
Lantzvillian
Member
 
Registered: Oct 2007
Location: BC, Canada
Distribution: Fedora, Debian
Posts: 210

Original Poster
Rep: Reputation: 41
gotter thanks everyone
 
Old 10-17-2007, 09:00 AM   #6
juergen
Member
 
Registered: Sep 2001
Posts: 40

Rep: Reputation: 16
Another sed approach:
Code:
sed '1,5s/x/y/'
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
find awk sed.. something along these lines citrus Linux - General 1 08-21-2006 03:04 PM
awk/gawk/sed - read lines from file1, comment out or delete matching lines in file2 rascal84 Linux - General 1 05-24-2006 09:19 AM
Replacing a place holder on a template with bash, sed, and or awk rignes Programming 7 02-16-2006 04:20 PM
awk text that is on several lines homey Programming 2 10-31-2004 09:27 AM
replacing specific lines in a text document stellarmarine1 Linux - General 1 09-07-2004 02:34 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:28 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration