Hi
I have been working on an assignment and have run into some problems. The assignment is an awk script that splits the shell script into BEGIN and END. In particular, the problem is in the END section with the sorting of a numeric array. I have tried the following in a for-loop(posted):
TotalSales[n] | "sort -n"

The only promblem is that they get sorted by letter and not by number as intended.

Current output is:

Lazy Acres, Inc.
2009 Sales Associates Ranking
Name Position Sales Amount
============================================

Buck, Fast - - Stock Boy - 2630.78
Doe, John - Clerk - 134.01
Lindon, Rosemary - Producer - 31.00
Miller, Dennis - Commedian - 9.90
Rush, George - Salesman - 1049.79
Worker, Susan - Manager - 360.00


Example of correct output:

Lazy Acres, Inc.2009 Sales Associates Ranking
Name Position Sales Amount
==========================================
Buck, Fast - Stock Boy - 2630.78
Rush, George - Salesman - 1049.79
Worker, Susan - Manager - 360.00
Doe, John - Clerk - 134.01
Lindon, Rosemary - Producer - 31.00
Miller, Dennis - Commedian - 9.90


Does anyone have any hints or suggestions ?

Thanks in advance.

for( n=21; n<=26; n++ )
 printf "%-8s %-10s %6s\t  %10.2f\n", lname[n]", ", fname[n], position[n], TotalSales[n] | "sort -n"

Recommended Answers

All 6 Replies

Hello,

The problem is you are not telling sort which column to sort off of. By default is uses the first column. The -k flag in sort tells it what field to use for the sort and the = t flag tells it the separator. You could try:

sort -n  -t - -k 3

You may have trouble with the - as the separator so you could substitute : to separate the info and then replace it with sed afterward.

Hi
I tried the example code and the script still manages to display incorrectly.

The modified code looks like this.

 for( n=21; n<=26; n++ )
         { 
          printf "%-8s %-10s %6s\t %10.2f\n", lname[n]", ", fname[n],
    position[n], TotalSales[n] | "sort -n -k 3"
           } 

The output is the following:

Lazy Acres, Inc.
2009 Sales Associates Ranking
Name                Position    Sales Amount
============================================

Buck,    Fast       Stock Boy       2630.78
Doe,     John       Clerk            134.01
Lindon,  Rosemary   Producer          31.00
Miller,  Dennis     Commedian          9.90
Rush,    George     Salesman        1049.79
Worker,  Susan      Manager          360.00

Hi
I tried the example code and the script still manages to display incorrectly.

The modified code looks like this.

 for( n=21; n<=26; n++ )
         { 
          printf "%-8s %-10s %6s\t %10.2f\n", lname[n]", ", fname[n],
    position[n], TotalSales[n] | "sort -n -k 3"
           } 

The output is the following:

Lazy Acres, Inc.
2009 Sales Associates Ranking
Name                Position    Sales Amount
============================================

Buck,    Fast       Stock Boy       2630.78
Doe,     John       Clerk            134.01
Lindon,  Rosemary   Producer          31.00
Miller,  Dennis     Commedian          9.90
Rush,    George     Salesman        1049.79
Worker,  Susan      Manager          360.00

end quote.

By default sort uses white space (any space or tab) to separate fields so -k 3 would sort Stock, Clerk, Producer, etc. Is it possible to move the column you want to sort to the front of the list?

The other thing I just noticed is that you are running the sort on each individual line. AWK is parsing the file one record at a time and by piping inside awk you are only sorting the current record. Your could try sorting the output before you pass it to awk or put the sort outside the for loop.

By default sort uses white space (any space or tab) to separate fields so -k 3 would sort Stock, Clerk, Producer, etc. Is it possible to move the column you want to sort to the front of the list?

The other thing I just noticed is that you are running the sort on each individual line. AWK is parsing the file one record at a time and by piping inside awk you are only sorting the current record. Your could try sorting the output before you pass it to awk or put the sort outside the for loop.

Hi

Yes, I was recently told the sort does work in a loop. The only problem is that there is a different number of columns for each line being printed. Out of all the lines that are printed only one is greater in terms of number of columns. I was also told that sort has an option where it sorts according to the line and not a certain column. Is this true ?

Also, thank you for the help that you've been giving me.

Sort will work inside the loop but only for the data it is passed via the pipe which in this case is one record at a time. Why not output the file with the number at the front and then do a sort -n and it will sort the first column numerically.

Ca67 I was wondering if you could post the rest of the code so that I could see how the arrays are being generated.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.