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.
Thanks Richard
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.
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:#'
Ted. (PS: The spillover of "file" is due to my mailer line length)
-------------------------------------------------------------------- E-Mail: (Ted Harding) ted.harding@wlandres.net Fax-to-email: +44 (0)870 094 0861 Date: 17-Feb-11 Time: 14:40:39 ------------------------------ XFMail ------------------------------
On Thu, Feb 17, 2011 at 02:40:42PM -0000, Ted Harding wrote:
On 17-Feb-11 13:30:53, Brett Parker wrote:
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:#'
Thanks both of you for your helpful input.
Richard
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 ;)
On Thu, Feb 17, 2011 at 02:53:31PM +0000, Brett Parker wrote:
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!
Thanks. Unfortunately, the format of the data isn't my choice.
Thanks again. Richard
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:#'
EWRONG!
The last sed there should be...
sed -e 's#:([0-9][0-9][0-9][0-9])-([0-9][0-9])-([0-9][0-9]):#:\3/\2/\1:#'
So there, ner ner ner ner ner ner!