TechTalkz.com Logo Ask the Experts!

Go Back   TechTalkz.com Technology & Computer Troubleshooting Forums > Tech Support Archives > Linux & Opensource

Sorting confusion

Linux & Opensource

 
 
Thread Tools Display Modes
Unread 09-12-2007, 12:58 PM   #1
C.
Guest
 
Posts: n/a
Sorting confusion

Hi all,

I'm having a problem using 'sort' to pre-process log files. I'm not
getting my output sorted as I expect:

Given an input set:

2007-12-04 20:33:10.587532 "35:1196800053:389002" a
2007-12-04 20:30:25.384069 "35:1196800053:389002" b
2007-12-04 20:33:25.384069 "25:1196800053:389002" c

(fields are date, time, session id, something else)

I want to sort by the session id then by the time:

sort -t \t -k 3,3 -k 1,2 <test
2007-12-04 20:30:25.384069 "35:1196800053:389002" b
2007-12-04 20:33:10.587532 "35:1196800053:389002" a
2007-12-04 20:33:25.384069 "25:1196800053:389002" c

I would expect the last entry to be first ("25...<"35...) instead
output seems to sorted on k2.

It not due to sort getting confused about the order of keys:

sort -t \t -k 3,3 <test
2007-12-04 20:30:25.384069 "35:1196800053:389002" b
2007-12-04 20:33:10.587532 "35:1196800053:389002" a
2007-12-04 20:33:25.384069 "25:1196800053:389002" c

lowest session id still comes last.

sort --version reports:
sort (GNU coreutils) 5.97
Copyright (C) 2006 Free Software Foundation, Inc.
This is free software. You may redistribute copies of it under the
terms of
the GNU General Public License <http://www.gnu.org/licenses/gpl.html>.
There is NO WARRANTY, to the extent permitted by law.

Written by Mike Haertel and Paul Eggert.

I've tried stripping the quotes around the session id to no avail. If
the session id comes in the first column it works OK.

I expect I'm doing something stoopid, but I can't for the life of me
see what.

(yes the seperators are all tabs).

Help!

C.

Sponsored Links
 
Unread 09-12-2007, 12:59 PM   #2
Peter Benie
Guest
 
Posts: n/a
Re: Sorting confusion

In article <68d9d72f-53ac-43a8-b39c-0678ebdb1fa3@d21g2000prf.googlegroups.com>,
C. <colin.mckinnon***********> wrote:
>sort -t \t -k 3,3 <test
>2007-12-04 20:30:25.384069 "35:1196800053:389002" b
>2007-12-04 20:33:10.587532 "35:1196800053:389002" a
>2007-12-04 20:33:25.384069 "25:1196800053:389002" c


-t \t sets the separator to 't', not to TAB.

If you do repeat the command by typing sort -t '^V^I' -k 3,3 <test
(where ^V is Ctrl+V and ^I is TAB) you'll find that it sorts correctly.

If you don't like having invisible formatting in your commands, use
sort -t "`echo -e '\t'`" -k 3,3 <test

Peter
 
Unread 09-12-2007, 02:27 PM   #3
newsnet@mellis.me.uk
Guest
 
Posts: n/a
Re: Sorting confusion

On Sun, 9 Dec 2007 04:17:44 -0800 (PST), "C."
<colin.mckinnon***********> wrote:

>Hi all,
>
>I'm having a problem using 'sort' to pre-process log files. I'm not
>getting my output sorted as I expect:
>
>Given an input set:
>
>2007-12-04 20:33:10.587532 "35:1196800053:389002" a
>2007-12-04 20:30:25.384069 "35:1196800053:389002" b
>2007-12-04 20:33:25.384069 "25:1196800053:389002" c
>
>(fields are date, time, session id, something else)
>
>I want to sort by the session id then by the time:
>
> sort -t \t -k 3,3 -k 1,2 <test
>2007-12-04 20:30:25.384069 "35:1196800053:389002" b
>2007-12-04 20:33:10.587532 "35:1196800053:389002" a
>2007-12-04 20:33:25.384069 "25:1196800053:389002" c
>
>I would expect the last entry to be first ("25...<"35...) instead
>output seems to sorted on k2.
>
>It not due to sort getting confused about the order of keys:
>
>sort -t \t -k 3,3 <test
>2007-12-04 20:30:25.384069 "35:1196800053:389002" b
>2007-12-04 20:33:10.587532 "35:1196800053:389002" a
>2007-12-04 20:33:25.384069 "25:1196800053:389002" c
>
>lowest session id still comes last.
>
>sort --version reports:
>sort (GNU coreutils) 5.97
>Copyright (C) 2006 Free Software Foundation, Inc.
>This is free software. You may redistribute copies of it under the
>terms of
>the GNU General Public License <http://www.gnu.org/licenses/gpl.html>.
>There is NO WARRANTY, to the extent permitted by law.
>
>Written by Mike Haertel and Paul Eggert.
>
>I've tried stripping the quotes around the session id to no avail. If
>the session id comes in the first column it works OK.
>
>I expect I'm doing something stoopid, but I can't for the life of me
>see what.
>
>(yes the seperators are all tabs).
>
>Help!
>
>C.


It is a shell problem not a sort problem. Try

IFS=\t;sort -k 3,3 <test

ME
 
Unread 09-12-2007, 03:10 PM   #4
newsnet@mellis.me.uk
Guest
 
Posts: n/a
Re: Sorting confusion

Sponsored Links
On Sun, 9 Dec 2007 04:17:44 -0800 (PST), "C."
<colin.mckinnon***********> wrote:

>Hi all,
>
>I'm having a problem using 'sort' to pre-process log files. I'm not
>getting my output sorted as I expect:
>
>Given an input set:
>
>2007-12-04 20:33:10.587532 "35:1196800053:389002" a
>2007-12-04 20:30:25.384069 "35:1196800053:389002" b
>2007-12-04 20:33:25.384069 "25:1196800053:389002" c
>
>(fields are date, time, session id, something else)
>
>I want to sort by the session id then by the time:
>
> sort -t \t -k 3,3 -k 1,2 <test
>2007-12-04 20:30:25.384069 "35:1196800053:389002" b
>2007-12-04 20:33:10.587532 "35:1196800053:389002" a
>2007-12-04 20:33:25.384069 "25:1196800053:389002" c
>
>I would expect the last entry to be first ("25...<"35...) instead
>output seems to sorted on k2.
>
>It not due to sort getting confused about the order of keys:
>
>sort -t \t -k 3,3 <test
>2007-12-04 20:30:25.384069 "35:1196800053:389002" b
>2007-12-04 20:33:10.587532 "35:1196800053:389002" a
>2007-12-04 20:33:25.384069 "25:1196800053:389002" c
>
>lowest session id still comes last.
>
>sort --version reports:
>sort (GNU coreutils) 5.97
>Copyright (C) 2006 Free Software Foundation, Inc.
>This is free software. You may redistribute copies of it under the
>terms of
>the GNU General Public License <http://www.gnu.org/licenses/gpl.html>.
>There is NO WARRANTY, to the extent permitted by law.
>
>Written by Mike Haertel and Paul Eggert.
>
>I've tried stripping the quotes around the session id to no avail. If
>the session id comes in the first column it works OK.
>
>I expect I'm doing something stoopid, but I can't for the life of me
>see what.
>
>(yes the seperators are all tabs).
>
>Help!
>
>C.


It is a shell problem not a sort problem. Try

IFS=\t;sort -k 3,3 <test

ME

Sponsored Links
 
 

Thread Tools
Display Modes



< Home - Windows Help - MS Office Help - Hardware Support >


New To Site? Need Help?

All times are GMT. The time now is 09:37 PM.


vBulletin, Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © 2005-2016, TechTalkz.com. All Rights Reserved - Privacy Policy
Valid XHTML 1.0 Transitional