LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 04-19-2011, 05:37 AM   #1
lulwot
LQ Newbie
 
Registered: Apr 2011
Posts: 2

Rep: Reputation: 0
Linux bash script: sort command - not sorting properly


---

Last edited by lulwot; 04-21-2011 at 06:59 AM.
 
Old 04-19-2011, 06:17 AM   #2
Robhogg
Member
 
Registered: Sep 2004
Location: Old York, North Yorks.
Distribution: Debian 7 (mainly)
Posts: 653

Rep: Reputation: 85
I believe you need "-k3" or "--key=3", rather than just "-3".

man sort will show you the parameters accepted by the command.
 
1 members found this post helpful.
Old 04-19-2011, 06:21 AM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950
The old "+1" syntax isn't generally used by gnu sort, although it does still support it. Instead, use the -k option, as explained in the man and info pages.
Code:
sort -t "/" -k 3 logfile
This will sort from the start of the 3rd field to the end of the line. To force it to sort only within that field, use -k 3,3.

According to the above, the 3rd field delimited by / is (or at least starts with) the year. Is that what you want to sort by? You need to use -n or one of sort's other numerical sort options if you want anything other than a lexical sort.

I highly recommend reading the info page for this command.
 
1 members found this post helpful.
Old 04-19-2011, 08:28 PM   #4
lulwot
LQ Newbie
 
Registered: Apr 2011
Posts: 2

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by David the H. View Post
The old "+1" syntax isn't generally used by gnu sort, although it does still support it. Instead, use the -k option, as explained in the man and info pages.
Code:
sort -t "/" -k 3 logfile
This will sort from the start of the 3rd field to the end of the line. To force it to sort only within that field, use -k 3,3.

According to the above, the 3rd field delimited by / is (or at least starts with) the year. Is that what you want to sort by? You need to use -n or one of sort's other numerical sort options if you want anything other than a lexical sort.

I highly recommend reading the info page for this command.
thank you heaps! it was actually the 4th field i was after , thanks again.
 
  


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
Bash script sorting multple columns together KBriggs Linux - Newbie 2 08-13-2009 01:59 PM
bash scripting: sorting and using using multiple files in a script daberkow Linux - Newbie 8 05-28-2009 10:24 AM
Need help with bash script /simplified sort command/ szcs Linux - General 0 10-26-2008 12:40 PM
sorting text file - sort command man_linux Linux - General 16 08-09-2006 05:58 PM


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