Is there a way to ignore header lines in a UNIX sort?

后端 未结 12 2211
余生分开走
余生分开走 2020-11-28 21:02

I have a fixed-width-field file which I\'m trying to sort using the UNIX (Cygwin, in my case) sort utility.

The problem is there is a two-line header at the top of t

12条回答
  •  一向
    一向 (楼主)
    2020-11-28 21:26

    So here's a bash function where arguments are exactly like sort. Supporting files and pipes.

    function skip_header_sort() {
        if [[ $# -gt 0 ]] && [[ -f ${@: -1} ]]; then
            local file=${@: -1}
            set -- "${@:1:$(($#-1))}"
        fi
        awk -vsargs="$*" 'NR<2{print; next}{print | "sort "sargs}' $file
    }
    

    How it works. This line checks if there is at least one argument and if the last argument is a file.

        if [[ $# -gt 0 ]] && [[ -f ${@: -1} ]]; then
    

    This saves the file to separate argument. Since we're about to erase the last argument.

            local file=${@: -1}
    

    Here we remove the last argument. Since we don't want to pass it as a sort argument.

            set -- "${@:1:$(($#-1))}"
    

    Finally, we do the awk part, passing the arguments (minus the last argument if it was the file) to sort in awk. This was orignally suggested by Dave, and modified to take sort arguments. We rely on the fact that $file will be empty if we're piping, thus ignored.

        awk -vsargs="$*" 'NR<2{print; next}{print | "sort "sargs}' $file
    

    Example usage with a comma separated file.

    $ cat /tmp/test
    A,B,C
    0,1,2
    1,2,0
    2,0,1
    
    # SORT NUMERICALLY SECOND COLUMN
    $ skip_header_sort -t, -nk2 /tmp/test
    A,B,C
    2,0,1
    0,1,2
    1,2,0
    
    # SORT REVERSE NUMERICALLY THIRD COLUMN
    $ cat /tmp/test | skip_header_sort -t, -nrk3
    A,B,C
    0,1,2
    2,0,1
    1,2,0
    

提交回复
热议问题