2

I have the data look like this

$ cat file
(71149, 3079, 68070, 0.0433) Alex
(51135, 2881, 48254, 0.0563) Brandon
(27231, 7105, 20126, 0.2609) Chad
(8365, 3634, 4731, 0.4344) Daniel
(7490, 7346, 144, 0.9808) Eliz
(6841, 3917, 2924, 0.5726) Frank
(6740, 7393, -653, 1.0969) Gates
(5084, 500, 4584, 0.0983) Harry
(5044, 3913, 1131, 0.7758) Ian
(4760, 699, 4061, 0.1468) Jack
....

I want to sort the content by the last element in the bracket, how can i do it in command line.

$ cat file | ... magic ... 
(6740, 7393, -653, 1.0969) Gates
(7490, 7346, 144, 0.9808) Eliz
(5044, 3913, 1131, 0.7758) Ian
(6841, 3917, 2924, 0.5726) Frank
(8365, 3634, 4731, 0.4344) Daniel
(27231, 7105, 20126, 0.2609) Chad
(4760, 699, 4061, 0.1468) Jack
(5084, 500, 4584, 0.0983) Harry
(51135, 2881, 48254, 0.0563) Brandon
(71149, 3079, 68070, 0.0433) Alex
....

I succeeded in sorting the number but I don't know how to print the whole line in awk.

$cat file | sed 's/)//g' | awk 'BEGIN{FS=" "}{print $4}' | sort -r
5.1246
4.3936
2.7811
2.5
1.9
....

1 Answer 1

5
kent$  sort  -k4nr file
(6740, 7393, -653, 1.0969) Gates
(7490, 7346, 144, 0.9808) Eliz
(5044, 3913, 1131, 0.7758) Ian
(6841, 3917, 2924, 0.5726) Frank
(8365, 3634, 4731, 0.4344) Daniel
(27231, 7105, 20126, 0.2609) Chad
(4760, 699, 4061, 0.1468) Jack
(5084, 500, 4584, 0.0983) Harry
(51135, 2881, 48254, 0.0563) Brandon
(71149, 3079, 68070, 0.0433) Alex
Sign up to request clarification or add additional context in comments.

3 Comments

the default field separator should be white space right? then the fourth element is actually "1.0969)" which include the brackets... you can still sort even with weird characters in the field?
@B.Mr.W. so long as there are always 4 decimal places in that column it should work fine.
@B.Mr.W. Well a more consistent sort would be cat file | tr -d '(),' | sort -k4nr if you're more interested in simply viewing the results and the particular output formatting isn't important.

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.