I have a file with four fields and an awk script that strips out one field displaying the remaining three. I have added headings for each of these fields such as Player - Year - RBIs then below it comes the data. What I am trying to do is sort the RBIs field in my script from most to least at the shell prompt using the sort command ...however, the headings are also getting sorted! How can I disclude (heh, not even a word, but sounds right) the three headings from my sort?
So I tried your solution and it does sort the file properly, however my heading does not show, rather line 4 (out of 6 lines) repeats itself at the top. What to do? thinking, thinking, thinking....
I've tried using an echo statement before the BEGIN command but that just repeats at the bottom over and over totaling the number of lines I have in the file.
Is there anyway to sort per line number in the script and then the field #?
Thanks Shamrock, I ended up having to edit the source file to add the three headings then run this code that was presented to me earlier to get things to work:
head -2 "file" ; awk -f "script" "file" | sed -n '3,$p' | sort -k 3nr
I was thinking there must be a way to work with the script alone on this issue and your solution worked great. Thank you.
Glad that the solution works for you. Just my 2c - avoid doing formatting before data processing. You are first formatting (using printf) and then processing the formatted data (using sort). Tommorrow if you change your formatting printf, you will be forced to change your sort routine too.