Re: sed/awk convert lines to colum
Tim Chase <[email protected]>
| Newsgroups | gmane.editors.sed.user |
|---|---|
| Message-ID | <[email protected]> |
On 12/04/12 06:07, MOKRANI Rachid wrote:
> id ; text
> 1 ; AAAA
> 1 ; AA BB CC
> 1 ; CC 22 2DS
> 1 ; 45 H DE T
> 2 ; WW NN FDRET
> 2 ; ZZZ DS 05 K
> 2 ; ss sss ss ss ssss
> 3 ; 7584455 tedr dr
> 3 ; JH CD FER GT 544 . 45
> ...
>
> I would like to have with sed/awk the following result
>
> id ; text1 ; text2 ; text3
> ; text4 ; text5 ; text6 .......
> 1 ; AAAA ; AA BB CC ; CC 22 2DS
> ; 45 H DE T
> 2 ; WW NN FDRET ; ZZZ DS 05 K ; ss sss ss ss ssss
> 3 ; 7584455 tedr dr; JH CD FER GT 544 . 45
I *think* you want to take any adjacent lines that start with the
same ID and combine them into one line, separated by additional
semicolons. However, somehow you magically are adding an arbitrary
number of headers before you've seen the rest of the file. If it's
just combining the adjacent same lines, the following works for me:
sed -n -e '$!{:a;N;s/^\([0-9]\+\)\(.*\)\n\1\s/\1\2/;$p;ta;P;D;$p;ba}'
I suppose you could insert the headers manually if you knew many you
wanted to include:
sed -n -e"1s/.*/&; text2; text3; text4; text5; text6/" -e
'$!{:a;N;s/^\([0-9]\+\)\(.*\)\n\1\s/\1\2/;$p;ta;P;D;$p;ba}'
Beware that odd things happen if your file only contains two lines
(one header + one line of data).
-tkc