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

后端 未结 12 2230
余生分开走
余生分开走 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条回答
  •  猫巷女王i
    2020-11-28 21:44

    If you don't mind using awk, you can take advantage of awk's built-in pipe abilities

    eg.

    extract_data | awk 'NR<3{print $0;next}{print $0| "sort -r"}' 
    

    This prints the first two lines verbatim and pipes the rest through sort.

    Note that this has the very specific advantage of being able to selectively sort parts of a piped input. all the other methods suggested will only sort plain files which can be read multiple times. This works on anything.

提交回复
热议问题