LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-02-2005, 01:11 PM   #1
ltordsen
LQ Newbie
 
Registered: Aug 2005
Distribution: suse 9.3
Posts: 16

Rep: Reputation: 0
sorting problem


Hello.

I'm having a problem with the gnu sort. It boils down to this:

this sort will work fine:

(contents of file ignore_blanks)
a: a:1
a:b:2
b:a :1
b: b:10
b: b: 2

Note the spaces in lines.

When I use the commands

sort -b -t : ignore_blanks -k 1,1 -k 2,2 -k 3,3
and
sort -t : ignore_blanks -k 1,1 -bk 2,2 -bk 3,3

I get the same results (as I expect):
a: a:1
a:b:2
b:a :1
b: b:10
b: b: 2

But when I try a slight variation of files (i called this ignore_blanks2) and commands, things dont work as I expect:

(contents of ignore_blanks2)
a a 1
a b 2
b a 1
b b 10
b b 2

sort -k 1.1,1.1 -bk 1.3,1.4 -bk 1.6,1.7 ignore_blanks2
Result:
a a 1
a b 2
b b 2
b b 10
b a 1

Here, it does not ignore the leading white space in the specified fields. Anyone know a fix for this?
 
Old 11-02-2005, 01:19 PM   #2
ltordsen
LQ Newbie
 
Registered: Aug 2005
Distribution: suse 9.3
Posts: 16

Original Poster
Rep: Reputation: 0
sorry...the spaces didn't come through in the html of the post...it should look like (using ':space:' to denote 1 whitepace character

a:space:a:space::space::space:1
a:space:b:space::space::space:2
b:space:a:space::space::space:1
b:space::space:b:space:10
b:space::space:b:space::space:2

It is sorting by ascii value (as I would like), but it is not removing the leading whitespace in the second example. What am I doing wrong?
 
Old 11-02-2005, 03:25 PM   #3
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Apologies in advance that I'm not entirely clear what you're trying to do ...

... but, in general if you want to sort numbers, then you'll want to use "sort -n".

EXAMPLE:
Code:
cat list
1
  2
  3
  10
  11
  20
30
Code:
sort list  # Wrong!
1
  10
  11
  2
  20
  3
30

sort -n list   # Better...
1
  2
  3
  10
  11
  20
30

sort -n list | awk '{printf "%3d\n", $1}' # Best
  1
  2
  3
 10
 11
 20
 30
'Hope that helps .. PSM
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Which sorting algorithm? nodger Programming 6 01-28-2005 06:13 PM
sorting and stuff arnulfo Programming 13 01-01-2005 04:37 PM
Sorting Beppe83 Linux - Software 7 06-21-2004 09:10 AM
File sorting problem Kerridis Linux - General 3 03-08-2004 10:44 PM
Sorting question AMMullan Linux - General 12 01-19-2004 03:09 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:20 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration