Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i reorder a file by ascending order (column)?

Tags:

awk

I would like to reorder a whole file by ascending time order.

file.txt looks like this:

a 12.24 text

a 1.45 text

b 5.12 text

I would like it to look like this:

a 1.45 text

b 5.12 text

a 12.24 text
like image 736
Jordan Diaz Avatar asked Jun 24 '10 18:06

Jordan Diaz


People also ask

Which command will sort files in ascending order?

SORT command is used to sort a file, arranging the records in a particular order. By default, the sort command sorts file assuming the contents are ASCII. Using options in the sort command can also be used to sort numerically. SORT command sorts the contents of a text file, line by line.

How do you do ascending order in Python?

Python sorted() Function The sorted() function returns a sorted list of the specified iterable object. You can specify ascending or descending order. Strings are sorted alphabetically, and numbers are sorted numerically.


2 Answers

The sort command may fit your needs better than awk.

# sort -gk 2 test.txt 
a 1.45 text
b 5.12 text
a 12.24 text

-g compares them as numbers instead of strings. And -k 2 sorts on the second column.

like image 69
Robert Wohlfarth Avatar answered Oct 04 '22 20:10

Robert Wohlfarth


Use the sort linux programme, not awk. Precisely:

sort -n -k 2 <filename>
like image 43
mbq Avatar answered Oct 04 '22 21:10

mbq