LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
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 08-24-2009, 12:09 PM   #1
bridrod
Member
 
Registered: Aug 2009
Distribution: SLES, openSUSE
Posts: 39

Rep: Reputation: 15
SED and Replacing Specific Line


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
 
Old 08-24-2009, 12:11 PM   #2
pwc101
Senior Member
 
Registered: Oct 2005
Location: UK
Distribution: Slackware
Posts: 1,847

Rep: Reputation: 128Reputation: 128
Code:
sed '178s/whatyouwanttogetridof/whatyouwanttoreplaceitwith/' input.txt > output.txt
 
Old 08-24-2009, 12:19 PM   #3
bridrod
Member
 
Registered: Aug 2009
Distribution: SLES, openSUSE
Posts: 39

Original Poster
Rep: Reputation: 15
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?
 
Old 08-24-2009, 12:33 PM   #4
pwc101
Senior Member
 
Registered: Oct 2005
Location: UK
Distribution: Slackware
Posts: 1,847

Rep: Reputation: 128Reputation: 128
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
 
Old 08-24-2009, 01:15 PM   #5
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729
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)
 
Old 08-24-2009, 01:27 PM   #6
bridrod
Member
 
Registered: Aug 2009
Distribution: SLES, openSUSE
Posts: 39

Original Poster
Rep: Reputation: 15
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
 
Old 08-24-2009, 01:28 PM   #7
bridrod
Member
 
Registered: Aug 2009
Distribution: SLES, openSUSE
Posts: 39

Original Poster
Rep: Reputation: 15
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!
 
  


Reply

Tags
line, replace, sed, specific, whole


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 4 10-23-2013 02:48 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 10:02 PM.

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
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration