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
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.
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.
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.
Use the sort
linux programme, not awk
. Precisely:
sort -n -k 2 <filename>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With