LinuxQuestions.org
Help answer threads with 0 replies.
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 02-02-2010, 10:01 AM   #1
carolflb
Member
 
Registered: Oct 2009
Posts: 40

Rep: Reputation: 15
SED - substitute a word only in a certain line


Hi everybody,

I tried to use the command:

sed -i ''$line_number',/port/s/port/portMARKED/' file

to substitute the word "port" by the word "portMARKED" at the specific line that is saved in "line_number". The substitution happens, but it is copying my line twice... and I have no idea why...

I need to make a single substitution of one word for another at a certain line in my text file. Would somebody know how I could do that?

Thanks!
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 02-02-2010, 10:20 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
Hi,

sed "${line_number}s/port/portMARKED/" infile

I'm not sure why you added the ,/port/ part, it is not needed.

General form (substitute with address range): sed '[address1[,address2]]s/pattern/replacement/[flags]' infile

flags is not used in your example, but could be g (global replace on whole line).

Hope this helps.
 
2 members found this post helpful.
Old 02-02-2010, 10:27 AM   #3
amon
Member
 
Registered: May 2004
Location: UK
Distribution: Debian, Ubantu, CentOS
Posts: 146

Rep: Reputation: 17
Some handy sed examples: http://www-h.eng.cam.ac.uk/help/tpl/unix/sed.html

From that link:
Code:
 # substitute (find and replace) "foo" with "bar" on each line
 sed 's/foo/bar/'             # replaces only 1st instance in a line
 sed 's/foo/bar/4'            # replaces only 4th instance in a line
 sed 's/foo/bar/g'            # replaces ALL instances in a line
you have already seen the use of the
Code:
-i
option for inserting text to the file.
Not sure what the other parts were (or if you had got confused with the sed instructions as I often do).

if I take file (tmptxt.txt):
Code:
text1 text text
text2 text text
text3 text3 text3
And run:
Code:
sed -i '2 s/text/textMARKED/' tmptxt.txt
I get:
Code:
text1 text text
textMARKED2 text text
text3 text3 text3

in your script you may need to replace single quotes with double quotes.
 
2 members found this post helpful.
Old 02-02-2010, 10:30 AM   #4
carolflb
Member
 
Registered: Oct 2009
Posts: 40

Original Poster
Rep: Reputation: 15
Thanks a lot guys!

I saw the comma in another example, but now I understood why it should not be there.
 
  


Reply


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
print second word in 1st line along with 5th word in all the lines after the first bangaram Programming 5 08-31-2009 04:42 AM
substitute few words + change all the lines starting with a specific word + put blank rahmathullakm Programming 6 01-18-2009 12:35 PM
sed to delete a line for a word and line above cmontr Programming 11 07-03-2008 09:33 AM
How to Substitute Specific Keywords in a particular line joyds219 Linux - Newbie 4 04-04-2008 04:33 PM
Sed substitute for my username? camaroblue87 Linux - Newbie 1 04-29-2006 12:07 PM


All times are GMT -5. The time now is 08:05 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration