I am getting output from a program that first produces one line that is a bunch of column headers, and then a bunch of lines of data. I want to cut various columns of this output and view it sorted according to various columns. Without the headers, the cutting and sorting is easily accomplished via the -k option to sort along with cut or awk to view a subset of the columns. However, this method of sorting mixes the column headers in with the rest of the lines of output. Is there an easy way to keep the headers at the top?
Tell me more
×
Unix & Linux Stack Exchange is a question and answer site for
users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.
|
|
||||
|
Stealing Andy's idea and making it a function so it's easier to use:
Now I can do:
|
|||||||||||
|
|
You can keep the header at the top like this with bash:
Or do it with perl:
|
|||||||||||||||||
|
|
Hackish but effective: prepend
|
|||
|
|
|
Here's some magic perl line noise that you can pipe your output through to sort everything but keep the first line at the top: |
|||
|
|
|
I found a nice awk version that works nicely in scripts:
|
|||
|
|
{ head -1; sort; }to work. It always deletes a bunch of the text after the first line. Does anyone know why this happens? – jonderry Apr 23 '11 at 1:02headis reading more than one line into a buffer and throwing most of it away. Mysedidea had the same problem. – Andy Apr 23 '11 at 1:09