LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices

Reply
 
Search this Thread
Old 02-20-2007, 05:58 AM   #1
inonzi_prowler
LQ Newbie
 
Registered: Feb 2007
Location: London
Distribution: Kubuntu, Solaris
Posts: 4

Rep: Reputation: 0
Question getting sed to hold lines and print them out again- help please


hello all.

so i have this text file, a section of which looks like this:

Quote:
KH6:1:1
4691814
4691814
4691814
4691814
4691814
KT3:1:1
4691815
4691815
4691815
4691816
4691816
4691816
i would very very much like for it to look like this:

Quote:
KH6:1:1
KH6:1:1 4691814
KH6:1:1 4691814
KH6:1:1 4691814
KH6:1:1 4691814
KH6:1:1 4691814
KT3:1:1
KT3:1:1 4691815
KT3:1:1 4691815
KT3:1:1 4691815
KT3:1:1 4691816
KT3:1:1 4691816
KT3:1:1 4691816
reading around i guess i need to get it to hold the line in the buffer when it finds a ":" (the common bit of text in the left column) and print at the start of each line until it finds another ":".

is that right guys? i am very new to sed and am struggling.

many thanks in advance for any help.
 
Old 02-20-2007, 06:43 AM   #2
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,506

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Here is a simple awk solution:
Code:
$0 ~ /:/ { header = $0 ;
           print header
}
$0 !~ /:/ { print header, $0
}
I guess the first line of the input file contains a field like KH6:1:1, otherwise we have to parse the first lines of the input file in a different way.

Last edited by colucix; 02-20-2007 at 06:46 AM.
 
Old 02-20-2007, 06:47 AM   #3
Nick_Battle
Member
 
Registered: Dec 2006
Location: Bracknell, UK
Distribution: SUSE 13.1
Posts: 159

Rep: Reputation: 32
You can do this with sed, but it is very unintuitive. I would recommend awk:

/^K/ {
prefix=$0
print prefix
}
/^[0-9]/ {
print prefix, $0
}

Put that in a file xxx and run with "awk -f xxx yourdata"

HTH
 
Old 02-21-2007, 03:26 AM   #4
inonzi_prowler
LQ Newbie
 
Registered: Feb 2007
Location: London
Distribution: Kubuntu, Solaris
Posts: 4

Original Poster
Rep: Reputation: 0
thanks very much guys.
 
  


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
Join all lines using sed chipix Programming 3 04-03-2007 09:55 AM
Sed empty lines nc3b Programming 4 08-27-2006 06:37 AM
awk/gawk/sed - read lines from file1, comment out or delete matching lines in file2 rascal84 Linux - General 1 05-24-2006 09:19 AM
Help deleteing lines using Sed nazs Programming 5 03-07-2006 12:38 AM
sed and its hold buffer???????? 3inone Programming 2 05-07-2004 05:56 AM


All times are GMT -5. The time now is 11:37 AM.

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