D&C GLug - Home Page

[ Date Index ] [ Thread Index ] [ <= Previous by date / thread ] [ Next by date / thread => ]

Re: [LUG] Sort blocks of lines

 

Simon Avery wrote:
> Simon Williams wrote:
>> I'm looking for a slightly more advanced sort command. I have 2 extra 
>> things it needs to do. Firstly, it needs to be able to sort a file based 
>> on a date which looks a bit like this:
>> Tue Jan  8 04:53:47 GMT 2008  [and some other info which should be ignored]
> 
> Steve beat me to recommending Date::Manip, but it's excellent. It'll 
> read in a huge variety of humancentric date strings and convert to 
> unixtime, which can then be sorted with a basic sort();
> 
> Same probably exists for other languages, maybe even bash if you wanted 
> to avoid a called language entirely.
> 
>> Secondly, the file is in the format:
>> date/time as above
>> arbitrary text
>> blank line
>>
>> In this particular case the arbitrary text is all on one line so all I 
>> need to do is somehow convince the sort command to move blocks of 3 
>> lines around, but being able to tell it to move everything up to and 
>> including a blank line would also be nice.
> 
> In perl, split line into "unixdate,data\n", removing any bits you don't 
> want and push into an array, sort it, use it.

Hmmm. I really should get round to learning perl at some point. However, 
I did work out how to sort the date:
sort -rsn +2 -4 | sort -rsM +1 -2 | sort -rsn +5 -6

Now the only problem is how to get it to keep blocks of lines together. 
I think -z might be the answer, if I just add a \0 onto the end of every 
blank line then I should be in business. I'm thinking something like 
s/\n\n/\n\n\0/, but newlines and null characters are impossible to work 
with. Any suggestions?

-- 
The Mailing List for the Devon & Cornwall LUG
http://mailman.dclug.org.uk/listinfo/list
FAQ: http://www.dcglug.org.uk/linux_adm/list-faq.html