david_lyon has asked for the wisdom of the Perl Monks concerning the following question:
I want to convert the following matrix below Format 1), into the format in 2) with 4 column format going downwards, I am using the following perl code to do it and it works but is ugly. Can someone show me how is the proper way of doing it in perl. Thank You. Dave
Format 1) A C F SampleID Time ObsConc ObsConc ObsConc 5 24 2.27E+06 687.02 32521.94 5 168 1.92E+06 525.02 22198.44 5 12 2.94E+06 896.39 41331.61 5 -0.5 2.23E+06 942.34 40616.49 5 8 4.03E+06 1371.32 45863.69 6 24 1.02E+06 1057.89 46341.04 6 168 3.14E+06 4987.32 42166.08
Format 2) A 5 24 2.27E+06 A 5 168 1.92E+06 A 5 12 2.94E+06 A 5 -0.5 2.23E+06 A 5 8 4.03E+06 A 6 24 1.02E+06 A 6 168 ... C 5 24 ... C 5 168 ... C 5 12 ... C 5 -0.5 ... C 5 8 ... C 6 24 ... C 6 168 ... F 5 24 ... F 5 168 ... F 5 12 ... F 5 -0.5 ... F 5 8 ... F 6 24 ... F 6 168 ... etc...
foreach (3..$count_columns){ $header=`cut -f1,2,$_ $file | head -n1`; chomp $header; $cmd="cut -f1,2,$_ $file | grep -P \'^\\w+\' | grep -P -v \'^Sample +\' > $file\.2"; system($cmd); $cmd="perl -pi -e s\'/^/$header\\t/g\' $file\.2"; system($cmd); }
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: format conversion, please help
by hdb (Monsignor) on Apr 06, 2013 at 19:09 UTC | |
by david_lyon (Sexton) on Apr 06, 2013 at 19:37 UTC | |
by hdb (Monsignor) on Apr 06, 2013 at 21:19 UTC | |
Re: format conversion, please help
by ww (Archbishop) on Apr 06, 2013 at 19:04 UTC | |
Re: format conversion, please help
by Kenosis (Priest) on Apr 06, 2013 at 20:39 UTC | |
Re: format conversion, please help
by farang (Chaplain) on Apr 07, 2013 at 00:00 UTC |
Back to
Seekers of Perl Wisdom