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

后端 未结 12 2200
余生分开走
余生分开走 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:31

    In simple cases, sed can do the job elegantly:

        your_script | (sed -u 1q; sort)
    

    or equivalently,

        cat your_data | (sed -u 1q; sort)
    

    The key is in the 1q -- print first line (header) and quit (leaving the rest of the input to sort).

    For the example given, 2q will do the trick.

    The -u switch (unbuffered) is required for those seds (notably, GNU's) that would otherwise read the input in chunks, thereby consuming data that you want to go through sort instead.

提交回复
热议问题