LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 11-08-2006, 08:59 PM   #1
w3stfa11
LQ Newbie
 
Registered: Sep 2006
Posts: 9

Rep: Reputation: 0
Appending to a certain line in text


Is it possible to append to a certain line?

I have a list of things in a file followed by some numbers

record1 2 3 5
record2 2 3 4
record3 3 5 2

I'd like to append another number to record2. How would I do this?

I was trying out sed and /a, but it appends the text in a new line, which I don't want.

Thank you much.

Another similar question if y'all know. How can I edit the 2nd number of record1 (for example)?

Last edited by w3stfa11; 11-08-2006 at 09:01 PM.
 
Old 11-08-2006, 09:33 PM   #2
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 56
If the records can be accessed by line number, you could try this for the first part...
Code:
sed '2 s/.$/& new text here/' file.txt
and for the second part...
Code:
sed '1 s/./12/9' file.txt
 
Old 11-08-2006, 09:37 PM   #3
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,396
Blog Entries: 2

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
Code:
awk -e 'NR==2 {print $0, "another number";}' <YourFileName>
--- rod.
 
Old 11-08-2006, 10:17 PM   #4
w3stfa11
LQ Newbie
 
Registered: Sep 2006
Posts: 9

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by homey
If the records can be accessed by line number, you could try this for the first part...
Code:
sed '2 s/.$/& new text here/' file.txt
and for the second part...
Code:
sed '1 s/./12/9' file.txt
That works wonderfully. Thank you very much. I'm going to have to tweak the second part somehow because the numbers may have 1, 2, or 3 digits.

Thanks again!

Edit: Actually, I have one question. For the first part, it makes the changes and outputs it to stdout. How can I make the changes within the file?

Last edited by w3stfa11; 11-08-2006 at 11:07 PM.
 
Old 11-08-2006, 11:01 PM   #5
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 56
Edit: scratch that last idea on part 2
Gotta get some sleep.

Last edited by homey; 11-08-2006 at 11:20 PM.
 
Old 11-09-2006, 10:33 AM   #6
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,396
Blog Entries: 2

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
Quote:
Originally Posted by w3stfa11
Edit: Actually, I have one question. For the first part, it makes the changes and outputs it to stdout. How can I make the changes within the file?
Add the '-i' switch to your sed command line to perform edits 'in place'. Optionally, add a suffix to the -i switch to cause it to create a backup with the suffix appended as a filename extension.

man sed

--- rod.
 
  


Reply

Tags
bash, sed


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
C++ text file line by line/each line to string/array Dimitris Programming 15 03-11-2008 09:22 AM
how to change some text of a certain line of a text file with bash and *nix scripting alred Programming 6 07-10-2006 12:55 PM
appending line at the last line of a file .. vks_agra Linux - General 13 06-20-2006 08:51 PM
Appending Text Files From Bash Script alts Programming 3 11-18-2004 07:36 PM
PHP appending form fields and text. BigFred Programming 6 09-19-2003 11:02 AM


All times are GMT -5. The time now is 02:31 AM.

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