Using sort with awk script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using sort with awk script
# 1  
Old 12-13-2007
Using sort with awk script

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?

Trellot
# 2  
Old 12-13-2007
Quote:
Originally Posted by Trellot
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?

Trellot
For example, I have this:

BEGIN { printf("%8s%12s%9s\n", "Player", "Year", "RBIs")
printf("\n") }
{ printf("%8s%12s%10s\n", $1, $2, $4) }

then i type into the shell: awk -f "script" "file" | sort -k 3nr

but then my headings go to the bottom of the output. Can you sort specific lines within their respective field, thus leaving my headings alone?

hmm....

Last edited by Trellot; 12-13-2007 at 05:01 AM.. Reason: add question at end of post about line sorting
# 3  
Old 12-13-2007
Can't think of a clean solution here. You may wanna try:

Code:
head -1 "file" ; awk -f "script" "file" | sed -n '2,$p' | sort -k 3nr

HTH
# 4  
Old 12-13-2007
Quote:
Originally Posted by rikxik
Can't think of a clean solution here. You may wanna try:

Code:
head -1 "file" ; awk -f "script" "file" | sed -n '2,$p' | sort -k 3nr

HTH
Will try this, thank you.

Trellot
# 5  
Old 12-13-2007
Quote:
Originally Posted by Trellot
Will try this, thank you.

Trellot
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....

btw, what does the head -1 do?

Trellot
# 6  
Old 12-13-2007
Any ideas on how to sort my script without also sorting the headings? I just need field four to sort and just the numbers. Here is my script again:

BEGIN { printf("%8s%12s%9s\n", "Player", "Year", "RBIs")
printf("\n") }
{ printf("%8s%12s%10s\n", $1, $2, $4) }

when I sort at the shell...Player, Year and RBIs are sorted into the output.

Thanks all,

Trellot
# 7  
Old 12-13-2007
Quote:
Originally Posted by Trellot
Any ideas on how to sort my script without also sorting the headings? I just need field four to sort and just the numbers. Here is my script again:

BEGIN { printf("%8s%12s%9s\n", "Player", "Year", "RBIs")
printf("\n") }
{ printf("%8s%12s%10s\n", $1, $2, $4) }

when I sort at the shell...Player, Year and RBIs are sorted into the output.

Thanks all,

Trellot
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 #?
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Shell script to call and sort awk script and output

I'm trying to create a shell script that takes a awk script that I wrote and a filename as an argument. I was able to get that done but I'm having trouble figuring out how to keep the header of the output at the top but sort the rest of the rows alphabetically. This is what I have now but it is... (1 Reply)
Discussion started by: Eric7giants
1 Replies

2. Shell Programming and Scripting

awk pipe to sort

In the below awk to add a sort by smallest to largest should it be added after the END? Thank you :). BEGIN { FS="*" } # Read search terms from file1 into 's' FNR==NR { s next } { # Check if $5 matches one of the search terms for(i in s) { if($5 ~ i) { ... (4 Replies)
Discussion started by: cmccabe
4 Replies

3. Homework & Coursework Questions

Shell script/awk to sort text

1. The problem statement, all variables and given/known data: I have a file with a fragment of a novel, which I have to clear from punctuation and sort all the words contained one per line and non duplicated, all this going to a file called "palabras". Here is fragment of the input file: ... (4 Replies)
Discussion started by: ektorzoza
4 Replies

4. Shell Programming and Scripting

awk sort

input file abc1 abc23 abc12 abc15 output abc1 abc12 abc15 abc23 (9 Replies)
Discussion started by: yanglei_fage
9 Replies

5. Shell Programming and Scripting

Awk script to sort and display total occurancies

Hi I am not a good awk programer but the below code serving my purpose Input process 12936 user-name 15u IPv4 7438309 0t0 TCP prdabc.cesi.com:42016->efh.ijk.com:50002 (ESTABLISHED) process 12942 user-name 13u IPv4 7764990 0t0 TCP abc.abc.com:9402->192.168.1.1:63919... (2 Replies)
Discussion started by: rakeshkumar
2 Replies

6. Shell Programming and Scripting

How do I sort two arrays using awk?

Hi How do I sort two arrays in a ascending order? I'm just doing this to teach myself a book my dad got me on unix. The arrays are A and B and both are including values taken from a file input by the user. How do I arrange the contents of both arrays so that they are both ordered from... (3 Replies)
Discussion started by: razrnaga
3 Replies

7. UNIX for Advanced & Expert Users

Script to sort the files and append the extension .sort to the sorted version of the file

Hello all - I am to this forum and fairly new in learning unix and finding some difficulty in preparing a small shell script. I am trying to make script to sort all the files given by user as input (either the exact full name of the file or say the files matching the criteria like all files... (3 Replies)
Discussion started by: pankaj80
3 Replies

8. Shell Programming and Scripting

Sort in AWK

Hi, I usually use Access to sort data however for some reason its not working. Our systems guys and myself cannot figure it out so ive tried to use AWK to do the sorting. The file is made up of single lines in the format ... (4 Replies)
Discussion started by: eknryan
4 Replies

9. Homework & Coursework Questions

awk sort help

1. The problem statement, all variables and given/known data: I dont know what I do wrong, I am trying to create shell programming database: I have this command first: && > $fname ... echo $Name:$Surname:$Agency:$Tel:$Ref: >> $fname then I have echo " Name Surname Agency Tel... (2 Replies)
Discussion started by: jeht
2 Replies
Login or Register to Ask a Question