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 11-06-2011, 04:05 AM   #1
gengwei89
LQ Newbie
 
Registered: Nov 2011
Posts: 3

Rep: Reputation: Disabled
sed command to replace line in text file


Hi all,

I am currently having a problem as i have to add data to a particular line in a text file, how do i actually do it using the sed command?

eg.
textfile.txt

0,1,2,3,4
2,8,7,5,6
7,8,9,5,6

how do I add an ",2" to the back of line 1?
Advice needed..Thanks
 
Old 11-06-2011, 04:28 AM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 12,269

Rep: Reputation: 1028Reputation: 1028Reputation: 1028Reputation: 1028Reputation: 1028Reputation: 1028Reputation: 1028Reputation: 1028
We're not here to solve problems, we're here to help you solve them.
What have you tried, why do you think it didn't work as you wanted ?.
 
Old 11-06-2011, 04:30 AM   #3
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,498

Rep: Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956
Try the substitution by adding ,2 to the whole matched line.
 
Old 11-06-2011, 06:19 AM   #4
gengwei89
LQ Newbie
 
Registered: Nov 2011
Posts: 3

Original Poster
Rep: Reputation: Disabled
Here is what i have tried.
I tried to extract whole of line 3 using head tail, then using sed i tried to substitute line 3 with "It is replaced." But when i run the program, i realize that the data in try.txt is being clear and is totally emptied. Can anyone point out what mistake have I made?
Code:
GOTLINE=$(head -3 try.txt | tail -1)#extracts the line need to update
echo $(sed -e "3s/$GOTLINE/It is replaced/" >try.txt)
try.txt
I am line 1
I am line 2
I am line 3

Last edited by gengwei89; 11-06-2011 at 06:20 AM.
 
Old 11-06-2011, 06:29 AM   #5
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,546
Blog Entries: 28

Rep: Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176
That's what the shell does with >try.txt, it begins by emptying the file. Have a look at sed's -i option.
 
Old 11-06-2011, 07:38 AM   #6
Juako
Member
 
Registered: Mar 2010
Posts: 202

Rep: Reputation: 84
Quote:
Originally Posted by gengwei89 View Post
Here is what i have tried.
I tried to extract whole of line 3 using head tail, then using sed i tried to substitute line 3 with "It is replaced." But when i run the program, i realize that the data in try.txt is being clear and is totally emptied. Can anyone point out what mistake have I made?
Code:
GOTLINE=$(head -3 try.txt | tail -1)#extracts the line need to update
echo $(sed -e "3s/$GOTLINE/It is replaced/" >try.txt)
try.txt
I am line 1
I am line 2
I am line 3
You are trying to filter the 3rd line twice: GOTLINE will return only the third line, so sed will never find a 3rd line.

Use one or another. If you want to "print only the 3rd line" besides modifying it, you can do it with sed too. Use the "-n" switch (supress output) and the p flag for s so it gets printed out.

The redirection isn't needed either. Follow catkin's suggestion (though usually you test first without -i to see you get the expected result).

Also:
Code:
info sed
Hope this helps
 
Old 11-06-2011, 08:10 AM   #7
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713
Quote:
Originally Posted by gengwei89 View Post
how do I add an ",2" to the back of line 1?
Code:
sed '1 s/.*/&,2/'
The "1" in the beginning tells sed to only do line 1, not all the lines (note that you can also use a regular expression in slashes instead of a line number, and you can have two line numbers or regular expressions separated by a comma to work on a range of lines). And then it substitutes the text in the line with the original text (represented by the "&" character) followed by ",2".
 
  


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
how do you replace text between two words in a whole file not just 1 line w/ sed/awk lityit Programming 5 11-04-2011 12:04 AM
[SOLVED] trying to insert text in the last line of a file with sed command.... Sayan Acharjee Linux - General 7 10-04-2010 05:00 AM
using sed to replace text on one line in a text file vo1pwf Linux - Newbie 5 06-24-2009 07:54 AM
command line edit -- global find/replace on text file w/o going into vi car182 Linux - Newbie 4 05-25-2006 05:42 PM
SED - replace text in file on specific line 3saul Linux - Software 1 03-04-2006 07:01 PM


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