LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices



Reply
 
Search this Thread
Old 12-11-2008, 07:35 AM   #1
market_garden
LQ Newbie
 
Registered: Dec 2008
Posts: 3

Rep: Reputation: 0
Attempting to append a line of text to the end of the previous line


Hi all,
This is my first post on this forum. I need to append a line of text to the end of the previous line. My data is as follows:

device hostname user user01 show
* deviceid:6d41fc
OK
device hostname user user02 show
* deviceid:d8c18e
OK
device hostname user user03 show
* deviceid:abd00d
OK

I need it in the following format and there are a few thousand lines of this:

device hostname user user01 show * deviceid:6d41fc
OK
device hostname user user02 show * deviceid:d8c18e
OK
device hostname user user03 show * deviceid:abd00d
OK

or even better:

device hostname user user01 show * deviceid:6d41fc
device hostname user user02 show * deviceid:d8c18e
device hostname user user03 show * deviceid:abd00d

Can anyone help with this?
 
Old 12-11-2008, 08:02 AM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729
Welcome to LQ!!

What have you tried? Once we know where you are, it's easier to give the right kind of advice.

SED will do this, and I'm pretty sure AWK will also.

With SED, the basic drill is this:
when you see "device hostname", send the line to the hold register(h), get the next line, and append to the hold register (H)
Then, get (g) the contents of hold, strip out the "\n" (newline), and print
The final command would key off of "OK" and simply remove the line.

Really good SED tutorial here: http://www.grymoire.com/Unix/Sed.html
 
Old 12-11-2008, 09:20 AM   #3
market_garden
LQ Newbie
 
Registered: Dec 2008
Posts: 3

Original Poster
Rep: Reputation: 0
Thanks pixellany,
I haven't really tried anything once I got to this stage as I was stumped. I'm new to Linux or at least text manipulation within Linux so I'm not familiar with sed or awk. I had never heard of sed until your post. vi is the only editor I've used but not in any great detail.
 
Old 12-11-2008, 12:05 PM   #4
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729
OK.....

SED and AWK are two traditional Unix "power tools". They even have their own O'Reilly book ("SED and AWK"). The site I linked has a lot of good stuff--not just SED. You also might want to look at the Bash Guide for Beginners---free at http://tldp.org

What can we do to be of further assistance?
 
Old 12-11-2008, 12:37 PM   #5
market_garden
LQ Newbie
 
Registered: Dec 2008
Posts: 3

Original Poster
Rep: Reputation: 0
Thanks a lot. I'm currently looking through the SED tutorial to work out how to get the text in the file into the format I require. Thanks again.
 
  


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 On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
bash programming-append single line to end of file pheasand Linux - General 4 02-28-2014 10:41 AM
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
Append variable string(s) at end of each line schaganti Linux - Newbie 2 10-19-2007 02:31 PM
bash: append string to end of line khairil Programming 6 02-27-2007 06:09 AM
batch append string to the end of a determined line in text files osio Programming 6 06-30-2005 10:28 AM


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