Convert header rows into

I want to put the 3 first lines into a single line separated by ;
I've tried to use Sed and Awk but without success. I'm new to Shell scripting.
Thanks in advance!

Input

112
DESAC_201309_OR_DJ10
DJ10
1234567890123;8
1234567890124;20
1234567890125;3

expected Output

112;DESAC_201309_OR_DJ10;DJ10
1234567890123;8
1234567890124;20
1234567890125;3

Based on some assumptions:

awk '!/\;/{s=s?s";"$0:$0;next}/\;/{$0=s?s"\n"$0:$0;s=""}1' file
awk '{ORS=(NR<3)?";":"\n"}1' file

Try also

awk 'NR<3{printf "%s;",$0;next}1' file
sed '1{N;N;s/\n/;/g;}' file

or

sed '1N;2N;3s/\n/;/g' file
1 Like

Thank you all, It all worked very fine.
There have been a slight modification on the expected data.
I need ";" at the end of each line

---------- Post updated at 06:15 AM ---------- Previous update was at 05:58 AM ----------

I found the solution :smiley:

 sed '1N;2N;3s/\n/;/g;s/.*/&;/' file

Good. Just for fun, you could shorten it a tiny bit with standard sed:

sed '1N;2N;3s/\n/;/g;s/$/;/' file

Slight change again!
No ";" at the end but 0; at the begining of the first line and then 3; for the rest!

 
0;112;DESAC_201309_OR_DJ10;DJ10
3;1234567890123;8
3;1234567890124;20
3;1234567890125;3

Quick fix:

sed '1N;2N;3s/\n/;/g;3!s/$/;/' file