Here's my version. It's pretty straightforward, but maybe Ondrej's solution is
far better for some reason?
#!/usr/bin/perl -w
use strict;
my $inputfile; my $outputfile;
$inputfile='myinfile.txt';
$outputfile='myoutfile.txt';
open (INFILE, "$inputfile") || die "Can't open input file\n";
open (OUTFILE, ">$outputfile") || die "Can't open output file\n";
while (<INFILE>) {
if (/^(\S+)\s+(\S+)\s+(\S+)\s+(\S+)/) { # reads into $1 $2 $3 $4, left to
right
print OUTFILE "$1 $4 $3 $2\n"; # Swap the ordering
} else {
print OUTFILE $_; # If you don't match the regex above, just print the
line.
}
}
close INFILE;
close OUTFILE;
Pedro A Reche Gallardo wrote:
> Hi all, I have a file with 20 columns of positive and negative decimal
> numbers (eg: 9.782 -8.983) separated by a black space, and everycolumn
> is marked on top with an single alphabet letter.
> This is an example.
>
> A D C B
> 9.782 -8.983 -3.483 -3.219
> 0.995 -0.330 9.994 -4.000
>
> and, I would like to reorder them to look as follows.
>
> A B C D
> 9.782 -3.219 -3.483 -8.893
> 0.995 -4.000 9.994 -0.330