Dear Frineds,
I have a flat file as follows
ABCD
ABDCWQE
POIERAS
ADSGASGFG
GHJKFHD
XBDFGHFGDH
POIU
IJPFG
AFGJFPGOU
A;DGUPGU
SFSDFDSDFHDSF
SDFGHSFDH
I want this column to be converted into row like follows
ABCD, ABDCWQE, POIERAS, ADSGASGFG, GHJKFHD, XBDFGHFGDH
POIU, IJPFG, AFGJFPGOU, A;DGUPGU, SFSDFDSDFHDSF, SDFGHSFDH
How can I do this?
Please guide me.
Thanks in advance
You can try like this....
cat temp.log | perl -lp00e 's/\n(?=(\w))/$1?", ":""/eg'
With AWK:
awk '$1=$1' RS= OFS=, infile
But the code above assumes no blanks in the fields.
If that assumption is wrong, use this:
awk '$1=$1' RS= FS='\n' OFS=, infile
Use nawk or /usr/xpg4/bin/awk on Solaris.
Dear Friend
I have tried awk '$1=$1' RS= FS='\n' OFS=, infile but the script named as rearng.sh gives following error
rearng.sh: 1=1: not found.
awk: Cannot find or open file infile.
The source line number is 1.
Could you post the content of your script?
You should substitute infile with your real filename.
Did you try the perl solution?
Dear Rado,
I have tried the solution given by you and its working perfect. Just to know, what if i want the output file to be pipe (|) separated instead of comma (,)?
And secondly as you have asked if I have tried perl. I dont know how to use perl for it. It would be really great if you guide me thro it as well.
Thank you for all your help.
Assuming you file is named file:
% cat file
ABCD
ABDCWQE
POIERAS
ADSGASGFG
GHJKFHD
XBDFGHFGDH
POIU
IJPFG
AFGJFPGOU
A;DGUPGU
SFSDFDSDFHDSF
SDFGHSFDH
With AWK:
% awk '$1=$1' RS= OFS=\| file
ABCD|ABDCWQE|POIERAS|ADSGASGFG|GHJKFHD|XBDFGHFGDH
POIU|IJPFG|AFGJFPGOU|A;DGUPGU|SFSDFDSDFHDSF|SDFGHSFDH
With Perl:
% perl -lp00e 's/\n(?=(\w))/$1?"|":""/eg' file
ABCD|ABDCWQE|POIERAS|ADSGASGFG|GHJKFHD|XBDFGHFGDH
POIU|IJPFG|AFGJFPGOU|A;DGUPGU|SFSDFDSDFHDSF|SDFGHSFDH