1#!/usr/local/bin/perl
2
3die "Usage: cat file.txt | swapCols.pl $#ARGV cols1 cols2\nColumns are coma separated list of one-base column indexes\n" if $#ARGV < 1;
4
5# Parse command line arguments
6@cols1 = split /,/, $ARGV[0];
7@cols2 = split /,/, $ARGV[1];
8die "Different number of columns in each argument" if( $#cols1 != $#cols2 );
9
10# Add indexes to swap
11for($i = 0 ; $i <= $#cols1 ; $i++ ) {
12	$c1 = $cols1[$i] - 1;
13	$c2 = $cols2[$i] - 1;
14	$idx[$c2] = $c1;
15	$idx[$c1] = $c2;
16}
17
18# Process SDTIN
19while( $l = <STDIN> ) {
20	chomp $l;
21	@t = split /\t/, $l;
22
23	for( $i = 0 ; $i <= $#t ; $i++ ) {
24		if( $i > 0 )			{ print "\t"; }
25
26		$j = $idx[$i];
27
28		# Should we swap this one?
29		if( $j ne '' )	{ print "$t[$j]"; }
30		else 			{ print "$t[$i]"; }
31	}
32	print "\n";
33}
34