For Programmers: Free Programming Magazines  


Home > Archive > Unix Shell Programming > December 2004 > Sorting a file question.









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Sorting a file question.
Admin

2004-12-23, 4:11 am

I want to sort a text file according to the last column. The last column
varies in size --- some lines have 5 words and some lines have 50 words in
it.
Below is a sample 4 lines from that file. The last column starts with the
words Events:, Desc:, Details.

- Minor Mon Dec 20 01:23:09 138.8.161.215 Event:
HTTP_Twiki_Search_CmdExec \nProtocol:
- Minor Mon Dec 20 01:23:53 138.8.161.214 Desc: TCP_Port_Scan
\nProtocol: TCP (6) \nSource--\n
- Minor Mon Dec 20 01:23:54 138.8.161.98 Details: TCP_Port_Scan
\nProtocol: TCP (6) \nSource--\n
- Minor Mon Dec 20 01:23:56 138.8.161.5 Event:
HTTP_Twiki_Search_CmdExec \nProtocol: TCP (6) \nSource--\n

I'll be grateful for any ideas.


John L

2004-12-23, 9:03 am


"Admin" <admin@admin.com> wrote in message news:95pyd.10783$Z47.9610@newsread2.news.atl.earthlink.net...
> I want to sort a text file according to the last column. The last column
> varies in size --- some lines have 5 words and some lines have 50 words in
> it.
> Below is a sample 4 lines from that file. The last column starts with the
> words Events:, Desc:, Details.
>
> - Minor Mon Dec 20 01:23:09 138.8.161.215 Event:
> HTTP_Twiki_Search_CmdExec \nProtocol:
> - Minor Mon Dec 20 01:23:53 138.8.161.214 Desc: TCP_Port_Scan
> \nProtocol: TCP (6) \nSource--\n
> - Minor Mon Dec 20 01:23:54 138.8.161.98 Details: TCP_Port_Scan
> \nProtocol: TCP (6) \nSource--\n
> - Minor Mon Dec 20 01:23:56 138.8.161.5 Event:
> HTTP_Twiki_Search_CmdExec \nProtocol: TCP (6) \nSource--\n
>
> I'll be grateful for any ideas.
>


That is the eighth field (and onwards), so: sort -b -k8

If it is not always the eighth field, then you could
use sed or perl or awk to rearrange each line, or
place a unique field separator immediately before
your "last column", then sort, then change the lines
back again.

--
John.
>



John L

2004-12-27, 8:56 pm


"Admin" <admin@admin.com> wrote in message news:95pyd.10783$Z47.9610@newsread2.news.atl.earthlink.net...
> I want to sort a text file according to the last column. The last column
> varies in size --- some lines have 5 words and some lines have 50 words in
> it.
> Below is a sample 4 lines from that file. The last column starts with the
> words Events:, Desc:, Details.
>
> - Minor Mon Dec 20 01:23:09 138.8.161.215 Event:
> HTTP_Twiki_Search_CmdExec \nProtocol:
> - Minor Mon Dec 20 01:23:53 138.8.161.214 Desc: TCP_Port_Scan
> \nProtocol: TCP (6) \nSource--\n
> - Minor Mon Dec 20 01:23:54 138.8.161.98 Details: TCP_Port_Scan
> \nProtocol: TCP (6) \nSource--\n
> - Minor Mon Dec 20 01:23:56 138.8.161.5 Event:
> HTTP_Twiki_Search_CmdExec \nProtocol: TCP (6) \nSource--\n
>
> I'll be grateful for any ideas.
>


That is the eighth field (and onwards), so: sort -b -k8

If it is not always the eighth field, then you could
use sed or perl or awk to rearrange each line, or
place a unique field separator immediately before
your "last column", then sort, then change the lines
back again.

--
John.
>



Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com