LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux > Linux - Newbie
User Name
Password
Linux - Newbie This 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

Tags used in this thread
Popular LQ Tags , , , , ,

Reply
 
Thread Tools
Old 08-24-2009, 12:09 PM   #1
bridrod
LQ Newbie
 
Registered: Aug 2009
Distribution: SLES, openSUSE, CentOS
Posts: 17
Thanked: 0
SED and Replacing Specific Line


[Log in to get rid of this advertisement]
I have a file that contains the following line:

include = [ * ]

That above line shows up three times in the file.

The line I just want to replace is line 178 of that file.

How can I do that with SED? The problem seems to rely on using brackets and "asterisks" since they are apparently used as command parameters as well.

I tried different combinations unsuccessfully.


TIA,

-Rod
windows_xp_2003 bridrod is offline  
Tag This Post , , , , ,
Reply With Quote
Old 08-24-2009, 12:11 PM   #2
pwc101
Senior Member
 
Registered: Oct 2005
Location: Southampton, UK
Distribution: Slackware
Posts: 1,698
Thanked: 93
Code:
sed '178s/whatyouwanttogetridof/whatyouwanttoreplaceitwith/' input.txt > output.txt
windows_xp_2003 pwc101 is offline     Reply With Quote
Thanked by:
Old 08-24-2009, 12:19 PM   #3
bridrod
LQ Newbie
 
Registered: Aug 2009
Distribution: SLES, openSUSE, CentOS
Posts: 17
Thanked: 0

Original Poster
Quote:
Originally Posted by pwc101 View Post
Code:
sed '178s/whatyouwanttogetridof/whatyouwanttoreplaceitwith/' input.txt > output.txt
Thanks for the quick reply, but it doesn't seem to work:

sed '178s/include = [ * ]/include = [ emc* ]' /etc/evms.test > /etc/emvs.test2
sed: -e expression #1, char 39: unterminated `s' command

Any idea?
windows_xp_2003 bridrod is offline     Reply With Quote
Old 08-24-2009, 12:33 PM   #4
pwc101
Senior Member
 
Registered: Oct 2005
Location: Southampton, UK
Distribution: Slackware
Posts: 1,698
Thanked: 93
You're missing a final / and you need to escape (precede with a \) the special characters (spaces, brackets, asterisks etc.):
Code:
sed '178s/include\ =\ \[\ \*\ \]/include\ =\ \[\ emc\*\ \]/' /etc/evms.test > /etc/emvs.test2
windows_xp_2003 pwc101 is offline     Reply With Quote
Thanked by:
Old 08-24-2009, 01:15 PM   #5
pixellany
Moderator
 
Registered: Nov 2005
Location: Pasadena, CA
Distribution: Arch
Posts: 13,130
Thanked: 281
Really good SED tutorial here:
http://www.grymoire.com/Unix/Sed.html

You can also avoid some of the escapes by using sed -r (turns on extended regular expressions)
linuxredhat pixellany is offline     Reply With Quote
Thanked by:
Old 08-24-2009, 01:27 PM   #6
bridrod
LQ Newbie
 
Registered: Aug 2009
Distribution: SLES, openSUSE, CentOS
Posts: 17
Thanked: 0

Original Poster
Quote:
Originally Posted by pwc101 View Post
You're missing a final / and you need to escape (precede with a \) the special characters (spaces, brackets, asterisks etc.):
Code:
sed '178s/include\ =\ \[\ \*\ \]/include\ =\ \[\ emc\*\ \]/' /etc/evms.test > /etc/emvs.test2
Yep! Right on! Worked like a charm! Thanks. I did not know I had to use the "\" for the spaces and other characters.

Question... In my example I had a specific line to change, but how would I do the same job if I did not know the specific line, but knew it's the second occurrence in the file? Or maybe only knew it's actually between a range of lines (i.e.: 155-190 lines)?

Thanks for your input. It really saved me time here!

-Rod
windows_xp_2003 bridrod is offline     Reply With Quote
Old 08-24-2009, 01:28 PM   #7
bridrod
LQ Newbie
 
Registered: Aug 2009
Distribution: SLES, openSUSE, CentOS
Posts: 17
Thanked: 0

Original Poster
Quote:
Originally Posted by pixellany View Post
Really good SED tutorial here:
http://www.grymoire.com/Unix/Sed.html

You can also avoid some of the escapes by using sed -r (turns on extended regular expressions)
Great link! Thanks!
windows_xp_2003 bridrod is offline     Reply With Quote

Reply

Bookmarks


Thread Tools

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
Sed append text to end of line if line contains specific text? How can this be done? helptonewbie Linux - Newbie 3 11-19-2008 02:26 PM
sed replacing line confusion freeindy Programming 12 08-07-2008 05:49 AM
Replacing text on specific lines with sed or awk? Lantzvillian Linux - Newbie 5 10-17-2007 10:00 AM
SED - replace text in file on specific line 3saul Linux - Software 1 03-04-2006 08:01 PM


All times are GMT -5. The time now is 01:05 AM.

Main Menu
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
RSS2  LQ Podcast
RSS2  LQ Radio
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration