Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash: sort text file by last field value

Tags:

I have a text file containing ~300k rows. Each row has a varying number of comma-delimited fields, the last of which is guaranteed numerical. I want to sort the file by this last numerical field. I can't do:

sort -t, -n -k 2 file.in > file.out 

as the number of fields in each row is not constant. I think sed, awk maybe the answer, but not sure how. E.g:

awk -F, '{print $NF}' file.in 

gives me the last column value, but how to use this to sort the file?

like image 747
Richard H Avatar asked Sep 30 '10 15:09

Richard H


People also ask

How do I sort a specific column in bash?

Sort allows us to sort a file by columns by using the -k option.

How do you sort in ascending order in bash?

Without any flags, the sort command sorts the file contents in ascending order by default. To reverse the sorting order, add the -r flag to the sort command, like this: sort -r fruits. txt . Sorting in reverse order by adding the -r flag applies to other examples in this tutorial.


2 Answers

Use awk to put the numeric key up front. $NF is the last field of the current record. Sort. Use sed to remove the duplicate key.

awk -F, '{ print $NF, $0 }' yourfile | sort -n -k1 | sed 's/^[0-9][0-9]* //' 
like image 95
Fred Foo Avatar answered Sep 19 '22 18:09

Fred Foo


vim file.in -c '%sort n /.*,\zs/' -c 'saveas file.out' -c 'q' 
like image 21
Benoit Avatar answered Sep 19 '22 18:09

Benoit