LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   getting sed to hold lines and print them out again- help please (https://www.linuxquestions.org/questions/linux-software-2/getting-sed-to-hold-lines-and-print-them-out-again-help-please-530680/)

inonzi_prowler 02-20-2007 05:58 AM

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.

colucix 02-20-2007 06:43 AM

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.

Nick_Battle 02-20-2007 06:47 AM

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

inonzi_prowler 02-21-2007 03:26 AM

thanks very much guys.


All times are GMT -5. The time now is 09:25 AM.