LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Linux bash script: sort command - not sorting properly (http://www.linuxquestions.org/questions/programming-9/linux-bash-script-sort-command-not-sorting-properly-875837/)

lulwot 04-19-2011 04:37 AM

Linux bash script: sort command - not sorting properly
 
---

Robhogg 04-19-2011 05:17 AM

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

man sort will show you the parameters accepted by the command.

David the H. 04-19-2011 05:21 AM

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.

lulwot 04-19-2011 07:28 PM

Quote:

Originally Posted by David the H. (Post 4329109)
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.


All times are GMT -5. The time now is 09:52 AM.