On 17 Feb 14:40, Ted Harding wrote:
On 17-Feb-11 13:30:53, Brett Parker wrote:
On 17 Feb 12:51, Richard Parsons wrote:
Hi everyone
I have some data like this:
something:01/01/1980:something something-else:12/05/1993:foobar another-something:32/01/2012:barfoo etc
In other words, it's a colon delimited file in which the second field is a date in the form dd/mm/yyyy.
How do I sort the whole file in date order by the second field? I can see that 'sort -k' allows me to pick the positional part of the fields and -t allows me to pick the delimiter. But I can't work out how to combine them so that the whole file is sorted.
sed -e 's#:([0-9][0-9])/([0-9][0-9])/([0-9][0-9][0-9][0-9]):#:\3-\2-\1:#' file | sort -d ":" -k 2
Should work. Note that's entirely untested and may eat your cat though.
It will eat the cat. There needs to be a pipe into a further 'sed' to undo the reversal of dd/mm/yyyy to yyyy/mm/dd:
sed -e 's#:([0-9][0-9])/([0-9][0-9])/([0-9][0-9][0-9][0-9]):#:\3-\2-\1:#' file | sort -d ":" -k 2 | sed -e 's#:([0-9][0-9])/([0-9][0-9])/([0-9][0-9][0-9][0-9]):#:\3-\2-\1:#'
Dates should be in YYYY-MM-DD format anyways - it's better, I deliberately didn't convert it back, it's less ambiguous this way. I was improving the input, obviously!
Sheesh ;)