3

My data looks like this:

Adelaide Crows      5        2       3       0       450    455     460.67  8      
Essendon            5        5       0       0       622    352     955.88  20    
Fremantle           5        3       2       0       439    428     598.50  12

As you can tell, there is a mixture of spaces and tabs. I need to be able to sort the last column descending. So the ouput looks like this:

Essendon            5        5       0       0       622    352     955.88  20  
Fremantle           5        3       2       0       439    428     598.50  12 
Adelaide Crows      5        2       3       0       450    455     460.67  8      

The whole data consists of all AFL teams.

Using sort how can I achieve this. Am i right in attempting to use the $ character to start from the end of line? I also need to sort the second last column after sorting the last column. Therefore any duplicate numbers in the last column will be sorted in the 2nd last column. Code so far:

sort -n -t$'\t' -k 9,9 -k 8,8 tmp

How do I take into account that the football team names will count as whitespace?

Here is the file being sorted (filename: 'tmp') sample data

1
  • I don't see evidence that your delimiters are a mix of tabs and spaces, what am I missing? You know about the sort -n option (for numeric). -k9,9n should help. Good luck. Commented May 16, 2013 at 17:59

1 Answer 1

7

You could copy the last field into the first position using awk first, then sort by the first field, the get rid of the first field using cut.

awk '{print($NF" "$0)}' sample.txt | sort -k1,1 -n -r -t' ' | cut -f2- -d' '

Port Adelaide       5        5       0       0       573    386     916.05  20    
Essendon            5        5       0       0       622    352     955.88  20    
Sydney Swans        5        4       1       0       533    428     681.68  16    
Hawthorn            5        4       1       0       596    453     620.64  16  
Richmond            5        3       2       0       499    445     579.68  12  
..
..
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @ravoori easy to understand answer. I was able to easily modify to sort by the second last column aswell! Thanks. Works perfectly :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.