LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 08-24-2009, 11:09 AM   #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, 11:11 AM   #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, 11:19 AM   #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, 11:33 AM   #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, 12:15 PM   #5
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
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, 12: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, 12: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



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 01:48 PM
sed replacing line confusion freeindy Programming 12 08-07-2008 04:49 AM
Replacing text on specific lines with sed or awk? Lantzvillian Linux - Newbie 5 10-17-2007 09:00 AM
SED - replace text in file on specific line 3saul Linux - Software 1 03-04-2006 07:01 PM

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

All times are GMT -5. The time now is 08:35 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