Search code examples
rubyperlawksedcut

Rearrange ranges of columns in a text file


I'm looking to rearrange some columns in a text file.

Basically I've got 32 columns and want 1-4,6-29,5,32

I can brute force this with awk, but that seems dumb. Any recommendations?


Solution

  • You can do a combination of awk and cut assuming the fields are separated by white space:

    awk '{$30=$5;$31=$32}1' file | cut -d' ' -f5,32 --complement
    

    For example:

    $ seq 32 | paste -s | 
      awk '{$30=$5;$31=$32}1' | 
      cut -d' ' -f5,32 --complement
    
    1 2 3 4 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 5 32