TechTalkz.com Logo

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

Notices

Sorting confusion

Linux & Opensource


Reply
 
Thread Tools Display Modes
Old 09-12-2007, 06:28 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.
  Reply With Quote
Old 09-12-2007, 06:29 PM   #2
Peter Benie
Guest
 
Posts: n/a
Re: Sorting confusion

In article <>,
C. <> 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
  Reply With Quote
Old 09-12-2007, 07:57 PM   #3
newsnet@mellis.me.uk
Guest
 
Posts: n/a
Re: Sorting confusion

On Sun, 9 Dec 2007 04:17:44 -0800 (PST), "C."
<> 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
  Reply With Quote
Old 09-12-2007, 08:40 PM   #4
newsnet@mellis.me.uk
Guest
 
Posts: n/a
Re: Sorting confusion

On Sun, 9 Dec 2007 04:17:44 -0800 (PST), "C."
<> 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
  Reply With Quote
Reply

Thread Tools
Display Modes



< Windows Help - MS Office Help - Hardware Support >


New To Site? Need Help?

All times are GMT +5.5. The time now is 06:39 PM.


vBulletin, Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO
Copyright © 2005-2009, TechTalkz.com. All Rights Reserved - Privacy Policy
Valid XHTML 1.0 Transitional