I want to sort lines consisting of dates, but I'm having trouble trying to figure out how to sort the lines and keep the lines whole. I also don't understand how to use pipe to sort the lines.
For example, my script receives this as a text file:
asdsa 24 asdsa 3 3000 054217542 30.3.2016
asdsadsa 25 asdsadsaa 5 4500 534215365 2.1.2014
dsasda 23 dsada 4 3200 537358234 6.3.2016
I would like to read line by line:
while read line; do
done < "$1"
And inside sort the lines by their dates. How can I sort the lines as a they are in a file, while I read them one by one?
What if I do this:
#!/bin/bash
PATH=${PATH[*]}:.
#filename: testScript
while read line; do
arr=( $line )
num_of_params=`echo ${#arr[*]}`
echo $line | sort -n -k$num_of_params
num_of_params=0
done < "$1"
My problem with this is that I actually send each line by its own to sort, and not the lines all together, but I don't know any other other way to do it (without using temp files, I'm not looking to use any of these).
Output:
asdsa 24 asdsa 3 3000 054217542 30.3.2016
asdsadsa 25 asdsadsaa 5 4500 534215365 2.1.2014
dsasda 23 dsada 4 3200 537358234 6.3.2016
Desired output:
asdsadsa 25 asdsadsaa 5 4500 534215365 2.1.2014
dsasda 23 dsada 4 3200 537358234 6.3.2016
asdsa 24 asdsa 3 3000 054217542 30.3.2016
As you can see, it didn't work.
How can I fix that?
Try
test
is the name of your file of course... It depends on the date being the last part of each line in the format you've specified in your initial post. (Tested on FreeBSD with (n)awk)Here is a solution using a Schwartzian transform with awk and
cut
:The awk part first splits the last field of the record,
$NF
(the date), at the periods into an arrayarr
:The second part prints the line with the reformatted date prepended: first the year, then the month and the day, the latter two with zero padding to two digits:
The output of this looks as follows:
Now we can just pipe to
sort
and use the first field:resulting in
And finally, we remove our inserted field again with
cut
, leaving only everything from the second field on:resulting in
A Bash solution
If instead of awk we want to use just Bash, we can do this:
The general idea is exactly the same: we add an extra column containing the reformatted date, sort by that and then remove it again.