---- Perl One-Liners ----

Re-organising input

Re-organising the column and row order of a text file can be easily accomplished with the -lane flags which rotate through lines and split input at white-space or other symbols specified by -F.
  1. Swap the order of the second and third column (indexed with 1 and 2, respectively, in a Perl array):
    perl -F"\t" -lane 'print(join "\t", @F[0,2,1,3..$#F])' input > output

    try it out with an example input file
  2. Bring the last column to the front in a comma separated file:
    perl -F, -lane 'print(join ",", @F[-1,0..$#F-1])' input > output
  3. Sort in decreasing order by values in the third column of a white-space delimited file:
    perl -lane '$s{$F[2]} .= $_; END {foreach (sort { $b <=> $a } keys %s) { print $s{$_}; }}' input > output
  4. Change gene coordinates, i.e. make sure start is always smaller than end coordinate:
    perl -lane '($F[3],$F[2]) = ($F[2],$F[3]) if ($F[3] < $F[2]); print(join "\t", @F);' input > output