I have the following command.

(head -1 invoice.txt  && sed '1d' invoice.txt |sort -rn -k4,4) |column -ts $'\t' |less -S
  • head -1 prints the first line.
  • sed "1d' removes the first line so soft wont be confused
  • sort -rn -k4,4 sorts my file by the 4th column only
  • column -ts $'\t' splits the file by tab
  • less -S prints it on the terminal

I would like to print only the first, fourth and last column. This is a small file however I have one with fourteen and one with twenty one columns, that i will use this template on.

And I am not sure if to use cut, awk, print or other command to pipe for my current output.

  • this is a working output, you need to remove the first line or the sorft will not work – Cueisa Apr 23 '19 at 8:31
  • Oh, sorry, I see what you're doing now. The formatting of the command confused me. – Kusalananda Apr 23 '19 at 8:36

You want to show only the 1st, 4th and last tab-delimited columns, and you want to sort the data on the second of those columns numerically, ignoring the header.

awk -F '\t' 'BEGIN { OFS=FS } { print $1, $4, $NF }' invoice.txt >tmpfile

    head -n 1 tmpfile
    sed '1d' tmpfile | sort -k 2,2rn
} | less -S

Or, you could bypass the temporary file and just pass everything from awk into the { ...; } compound command via a pipe. Add column -ts $'\t' before less -S if needed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.