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
Yoda
February 28, 2014, 11:14am
2
Based on some assumptions:
awk '!/\;/{s=s?s";"$0:$0;next}/\;/{$0=s?s"\n"$0:$0;s=""}1' file
Subbeh
February 28, 2014, 11:19am
3
awk '{ORS=(NR<3)?";":"\n"}1' file
RudiC
February 28, 2014, 11:20am
4
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
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