rearrange columns using awk or cut command

后端 未结 4 2070
闹比i
闹比i 2020-12-12 01:34

I have large file with 1000 columns. I want to rearrange so that last column should be the 3rd column. FOr this i have used,

cut -f1-2,1000,3- file > out.         


        
4条回答
  •  庸人自扰
    2020-12-12 02:23

    A shell wrapper function for awk' that uses simpler syntax:

    # Usage: rearrange int_n [int_o int_p ... ] < file
    rearrange () 
    { 
        unset n;
        n="{ print ";
        while [ "$1" ]; do
            n="$n\$$1\" \" ";
            shift;
        done;
        n="$n }";
        awk "$n" | grep '\w'
    }
    

    Examples...

    echo foo bar baz | rearrange 2 3 1
    bar baz foo 
    

    Using bash brace expansion, rearrange first and last 5 items in descending order:

    echo {1..1000}a | tr '\n' ' ' | rearrange {1000..995} {5..1}
    1000a 999a 998a 997a 996a 995a 5a 4a 3a 2a 1a 
    

    Sorted 3-letter shells in /bin:

    ls -lLSr /bin/?sh | rearrange 5 9 
    150792 /bin/csh 
    154072 /bin/ash 
    771552 /bin/zsh 
    1554072 /bin/ksh 
    

提交回复
热议问题