LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-13-2011, 02:55 AM   #1
sasanthi
LQ Newbie
 
Registered: Jul 2011
Posts: 11

Rep: Reputation: Disabled
sorting a first non-numerical column in a file


Dear all,

I have a file like:

ER- V67
ER+ V68
ER- V69
ER+ V70
ER+ V71
ER+ V72
ER+ V73
ER- V200
ER+ V240
ER- V241

and I would like to sort it according to first column, so having:

ER- V67
ER- V200
ER- V69
ER- V241
ER+ V71
ER+ V72
ER+ V73
ER+ V68
ER+ V240
ER+ V70

I am using the code:

sort -k1

but it prints it by sorting the second column. Could someone help me?

thanks!
 
Old 07-13-2011, 03:10 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,245

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
What makes you think it is sorting by the second column?
What output do you get?
 
Old 07-13-2011, 03:13 AM   #3
b0uncer
LQ Guru
 
Registered: Aug 2003
Distribution: CentOS, OS X
Posts: 5,131

Rep: Reputation: Disabled
I'm not quite sure if I follow you completely. So here's the data I use, from your post:

Code:
cat > /tmp/tmpfile << EOF
> ER- V67
> ER+ V68
> ER- V69
> ER+ V70
> ER+ V71
> ER+ V72
> ER+ V73
> ER- V200
> ER+ V240
> ER- V241
> EOF
Now sorting it:

Code:
sort /tmp/tmpfile
produces

ER+ V240
ER+ V68
ER+ V70
ER+ V71
ER+ V72
ER+ V73
ER- V200
ER- V241
ER- V67
ER- V69

which is sorted by the first column (ER+ before ER-) as far as I can tell. This is exactly the same as with

Code:
sort -k1
because the first field is the default one (i.e., you do not need to specify -k1 in this case). If you wanted ER- before ER+, use the -r option to reverse the results. What did you mean by sorting the second column, now?
 
Old 07-13-2011, 03:14 AM   #4
Diantre
Member
 
Registered: Jun 2011
Distribution: Slackware
Posts: 495

Rep: Reputation: 212Reputation: 212Reputation: 212
Please correct me if I'm not understanding exactly what you want to accomplish. A regular sort (no arguments) of you sample will return:

Code:
$ cat $FILE | sort
ER+ V240
ER+ V68
ER+ V70
ER+ V71
ER+ V72
ER+ V73
ER- V200
ER- V241
ER- V67
ER- V69
If you simply want the ER- lines to appear before ER+, just reverse the sort:

Code:
$ cat $FILE | sort -r
ER- V69
ER- V67
ER- V241
ER- V200
ER+ V73
ER+ V72
ER+ V71
ER+ V70
ER+ V68
ER+ V240
 
Old 07-13-2011, 03:26 AM   #5
sasanthi
LQ Newbie
 
Registered: Jul 2011
Posts: 11

Original Poster
Rep: Reputation: Disabled
I tried your replies but it does not work. I am getting the output:

ER- V200
ER+ V240
ER- V241
ER- V67
ER+ V68
ER- V69
ER+ V70
ER+ V71
ER+ V72
ER+ V73

which means that it is sorting it according to second column and the number.

I am confused!!!
 
Old 07-13-2011, 03:38 AM   #6
b0uncer
LQ Guru
 
Registered: Aug 2003
Distribution: CentOS, OS X
Posts: 5,131

Rep: Reputation: Disabled
Although it's probably far-fetched, better make sure you aren't using any distracting aliases, and see which version of sort you're using. To check that the sort command is not an alias, try

Code:
alias sort
(if it says "not found", it's not aliased--good) and if you're paranoid, run it with full path to make sure you're running what you think you're running. To check the version,

Code:
sort --version
which in my case says "sort (GNU coreutils) 5.93".

Your locale might also affect the sorting (I doubt this, but hey...), so you could check that as well with

Code:
locale
and in case it's something exotic, set LC_ALL to C to get the traditional behaviour; for example in Bash,

Code:
export LC_ALL=C
sets the locale (for that session). If it's not something exotic, that won't probably make a difference.

Last edited by b0uncer; 07-13-2011 at 03:49 AM.
 
Old 07-13-2011, 03:49 AM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,245

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
And to rule out other weirdness, its not a Windows file is it? Maybe run dos2unix on it to see if that helps too.
 
Old 07-13-2011, 04:25 AM   #8
sasanthi
LQ Newbie
 
Registered: Jul 2011
Posts: 11

Original Poster
Rep: Reputation: Disabled
yeap it works now! when run:
alias sort

i got the message 'not found', so that was bad. I did want you said and now it works.
thanks a lot!
 
Old 07-13-2011, 07:39 AM   #9
b0uncer
LQ Guru
 
Registered: Aug 2003
Distribution: CentOS, OS X
Posts: 5,131

Rep: Reputation: Disabled
Quote:
Originally Posted by sasanthi View Post
yeap it works now! when run:
alias sort

i got the message 'not found', so that was bad. I did want you said and now it works.
thanks a lot!
Err..."not found" means (here) that there is no such alias, meaning that the command "sort" is not aliased. This is how it should be (unless you specifically need it to be), and is what I expected. It is not an error, nor a problem, nor has anything to do with this case here (if there were, for some reason, an alias that caused unexpected behaviour of the sort command, then that would have been a problem).

Now could you be more specific about what exactly you did ("want you said [sic]"), i.e., what did you do differently than before to get the sort work as expected? I suspect that this was simply a case of bad luck with respect to the exact command issued, but it would clear things if you could for example specify which sort command exactly you used before, and after, to show the difference.

In any case, congratulations for solving it, and have a good day
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
[SOLVED] Sorting output by a column with numbers AND letters. velocity_girl Linux - Newbie 13 06-09-2011 02:03 AM
numerical operation on selected lines and column using AWK program vjramana Linux - Newbie 3 05-17-2010 12:43 AM
sorting a file by column AutoC Programming 6 08-07-2009 06:43 PM
Sorting by numerical order shoemoodoshaloo Linux - Newbie 3 07-21-2009 04:52 PM
Sorting of multi-column output rytrom Linux - Newbie 11 09-15-2003 12:31 PM


All times are GMT -5. The time now is 03:55 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration