Computing.Net > Forums > Unix > Limited sorting

Limited sorting

Reply to Message Icon

Original Message
Name: henrikleion
Date: July 21, 2005 at 07:44:07 Pacific
Subject: Limited sorting
OS: Solaris
CPU/Ram: sunblade 1500
Comment:

I want to sort a logfile that several processes are writing to. The tricky part is that I do not want to change the order of log messages coming from the same process:

<time> : <process> : <msg>:...
1154.805 : 14.03 : Log:UOutput:...
1154.805 : 14.04 : Log:Input:...
1154.805 : 14.03 : Alarm:UInt:...
1155.102 : 14.03 : Log:UOutput:...
1154.805 : 09.08 : Trace:Event:...

should become
1154.805 : 09.08 : Trace:Event : ...
1154.805 : 14.03 : Log:UOutput: ...
1154.805 : 14.03 : Alarm:UInt : ...
1154.805 : 14.04 : Log:Input : ...
1155.102 : 14.03 : Log:UOutput: ...

Note that the ordering between lines 2 and 3 has not changed.

<time> should be the first key, <process> the second and there should be no third key. So even if the first two keys are exactly the same, the ordering between them must not change.

I've tried "sort -t " : " -k 1,2 filelog" etc but I always end up with a fully sorted file. I had hoped that sort would do the trick but if it doesn't, nifty sed or awk algorithms are welcome

Thanks
/Henrik


Report Offensive Message For Removal


Response Number 1
Name: Jim Boothe
Date: July 21, 2005 at 11:30:06 Pacific
Reply: (edit)

The first pass adds an initial sequence.  With the added field, your sort fields are now 2 and 3 instead of 1 and 2.  The third sort key is the added sequence, for preserving the original sequence.  Then we cut that leading sequence field off.

seq=0
IFS=
while read line
do
((seq=seq+1))
echo $seq:$line
done < filelog |
sort -t: -k2b,2 -k3b,3 -k1n,1 |
cut -d: -f2-


Report Offensive Follow Up For Removal

Response Number 2
Name: henrikleion
Date: July 22, 2005 at 02:33:09 Pacific
Reply: (edit)

Aah, so simple.
Thanks Jim!


Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: Limited sorting

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software




Have you ever used OpenOffice?

Yes, as my main suite.
Yes, occationally.
Yes, but only once.
No, never.


View Results

Poll Finishes In 5 Days.
Discuss in The Lounge