Discussion:
sed/awk convert lines to colum
MOKRANI Rachid
2012-12-04 12:07:17 UTC
Permalink
Hi,

I have a file like :

My File.

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
...
...
...
...

Is someone has some idea for the good syntax command ?

Many thanks for some help.
Regards.
__________________________
Avant d'imprimer, pensez à l'environnement ! Please consider the environment before printing !
Ce message et toutes ses piÚces jointes sont confidentiels et établis à l'intention exclusive de ses destinataires. Toute utilisation non conforme à sa destination, toute diffusion ou toute publication, totale ou partielle, est interdite, sauf autorisation expresse. IFP Energies nouvelles décline toute responsabilité au titre de ce message. This message and any attachments are confidential and intended solely for the addressees. Any unauthorised use or dissemination is prohibited. IFP Energies nouvelles should not be liable for this message.
__________________________
Tim Chase
2012-12-04 12:30:34 UTC
Permalink
Post by MOKRANI Rachid
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
MOKRANI Rachid
2012-12-04 13:53:55 UTC
Permalink
Hi,

Many many thanks, it's working. I have an other suggestion.



And if I would like to reduce the number of text (text1 and text2) colums , do you have the magic command ?

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
1 ; AAAA ; AA BB CC (space or other) CC 22 2DS (space or other)45 H DE T
2 ; WW NN FDRET ; ZZZ DS 05 K (space or other) ss sss ss ss ssss
3 ; 7584455 tedr dr ; JH CD FER GT 544 . 45
...
...
...


Any suggestion.
Thanks in advance.
-----Message d'origine-----
Envoyé : mardi 4 décembre 2012 13:31
À : MOKRANI Rachid
Objet : Re: sed/awk convert lines to colum
Post by MOKRANI Rachid
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
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
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
__________________________
Avant d'imprimer, pensez à l'environnement ! Please consider the environment before printing !
Ce message et toutes ses pièces jointes sont confidentiels et établis à l'intention exclusive de ses destinataires. Toute utilisation non conforme à sa destination, toute diffusion ou toute publication, totale ou partielle, est interdite, sauf autorisation expresse. IFP Energies nouvelles décline toute responsabilité au titre de ce message. This message and any attachments are confidential and intended solely for the addressees. Any unauthorised use or dissemination is prohibited. IFP Energies nouvelles should not be liable for this message.
__________________________
Tim Chase
2012-12-04 14:33:49 UTC
Permalink
Post by MOKRANI Rachid
Many many thanks, it's working. I have an other suggestion.
Just as a point of English, I think you mean "I have another question"
Post by MOKRANI Rachid
And if I would like to reduce the number of text (text1 and text2) colums , do you have the magic command ?
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
1 ; AAAA ; AA BB CC (space or other) CC 22 2DS (space or other)45 H DE T
2 ; WW NN FDRET ; ZZZ DS 05 K (space or other) ss sss ss ss ssss
3 ; 7584455 tedr dr ; JH CD FER GT 544 . 45
Just take my
Post by MOKRANI Rachid
Post by Tim Chase
sed -n -e '$!{:a;N;s/^\([0-9]\+\)\(.*\)\n\1\s/\1\2/;$p;ta;P;D;$p;ba}'
And swallow the semicolon too:

sed -n -e '$!{:a;N;s/^\([0-9]\+\)\(.*\)\n\1\s*;/\1\2/;$p;ta;P;D;$p;ba}'

You can put your own punctuation (extra spaces or other delimiters)
after the "\2" if you need.

-tkc

Loading...