|
Home > Archive > Unix Shell > March 2006 > Simple but challenging sort problem
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Simple but challenging sort problem
|
|
| pleasedunreply@gmail.com 2006-03-15, 7:49 am |
| Hi all,
A csv file, each line starting with date like this:
1/11/2002,filed2,filed3....
as we can see, if we use the sort command to sort it, it will only sort
by day, not by year-month-day sequence.
any wise method to sort it in year-month-day orders?
Thanks very much.
| |
| Chris F.A. Johnson 2006-03-15, 7:49 am |
| On 2006-03-15, pleasedunreply@gmail.com wrote:
> Hi all,
>
> A csv file, each line starting with date like this:
>
> 1/11/2002,filed2,filed3....
Is that Nov. 1 or Jan. 11?
> as we can see, if we use the sort command to sort it, it will only sort
> by day, not by year-month-day sequence.
>
> any wise method to sort it in year-month-day orders?
My first advice, is, Always store dates in ISO format: 2002-11-01.
Then you can use sort. This script prepends a sane date to each
line, sorts it, then removes it, leaving the ugly date intact:
awk -F, '{ split($1,d,"/")
printf "%04d%02d%02d\t", d[3], d[2], d[1]
print
}' | sort | cut -f2-
I'd prefer:
awk -F, -v OFS=, '{ split($1,d,"/")
$1 = sprintf("%04d-%02d-%02d", d[3], d[2], d[1])
print
}' | sort
--
Chris F.A. Johnson, author | <http://cfaj.freeshell.org>
Shell Scripting Recipes: | My code in this post, if any,
A Problem-Solution Approach | is released under the
2005, Apress | GNU General Public Licence
| |
| Stephane Chazelas 2006-03-15, 7:49 am |
| On 15 Mar 2006 03:47:53 -0800, pleasedunreply@gmail.com wrote:
> Hi all,
>
> A csv file, each line starting with date like this:
>
> 1/11/2002,filed2,filed3....
>
> as we can see, if we use the sort command to sort it, it will only sort
> by day, not by year-month-day sequence.
[...]
sort -n -t / -k 3,3 -k 2,2 -k 1,1
(if it's mm/dd/yyyy)
--
Stephane
| |
| Stephane Chazelas 2006-03-15, 7:49 am |
| On 15 Mar 2006 12:30:33 GMT, Stephane Chazelas wrote:
> On 15 Mar 2006 03:47:53 -0800, pleasedunreply@gmail.com wrote:
> [...]
>
> sort -n -t / -k 3,3 -k 2,2 -k 1,1
>
> (if it's mm/dd/yyyy)
Sorry, I meant dd/mm/yyyy
--
Stephane
| |
| Xicheng 2006-03-15, 7:49 am |
| pleasedunreply@gmail.com wrote:
> Hi all,
>
> A csv file, each line starting with date like this:
>
> 1/11/2002,filed2,filed3....
>
> as we can see, if we use the sort command to sort it, it will only sort
> by day, not by year-month-day sequence.
>
> any wise method to sort it in year-month-day orders?
>
> Thanks very much.
"mon/day/year" ==>
sort -t/ -n -k3 -k1 -k2 myfile.txt
"day/mon/year" ==>
sort -t/ -n -k3 -k2 -k1 myfile.txt
Xicheng
| |
| Xicheng 2006-03-15, 5:55 pm |
| Xicheng wrote:
> pleasedunreply@gmail.com wrote:
sorry, shold be:
"mon/day/year" ==>[vbcol=seagreen]
> sort -t/ -n -k3 -k1 -k2 myfile.txt
sort -t/ -n -k3.1,3.4 -k1 -k2 myfile.txt
"day/mon/year" ==>
> sort -t/ -n -k3 -k2 -k1 myfile.txt
sort -t/ -n -k3.1,3.4 -k2 -k1 myfile.txt
Xicheng
| |
| SaltyBall 2006-03-15, 5:55 pm |
| Thanks all my friends, It save me hours of work to solve the problem
thanks again
(I am the guy who post the question)
pleasedunreply@gmail.com wrote:
> Hi all,
>
> A csv file, each line starting with date like this:
>
> 1/11/2002,filed2,filed3....
>
> as we can see, if we use the sort command to sort it, it will only sort
> by day, not by year-month-day sequence.
>
> any wise method to sort it in year-month-day orders?
>
> Thanks very much.
>
| |
| Jim Cochrane 2006-03-15, 8:48 pm |
| On 2006-03-15, SaltyBall <SaltyBall@Hell.com> wrote:
> Thanks all my friends, It save me hours of work to solve the problem
> thanks again
>
> (I am the guy who post the question)
You forgot to ask how much you owe them. [:-)]
[vbcol=seagreen]
>
>
> pleasedunreply@gmail.com wrote:
--
*** Free account sponsored by SecureIX.com ***
*** Encrypt your Internet usage with a free VPN account from http://www.SecureIX.com ***
| |
| Robert Bonomi 2006-03-16, 5:53 pm |
| In article <1142434424.518800.83510@u72g2000cwu.googlegroups.com>,
Xicheng <xicheng@gmail.com> wrote:
>
>sorry, shold be:
> "mon/day/year" ==>
>
>sort -t/ -n -k3.1,3.4 -k1 -k2 myfile.txt
>
> "day/mon/year" ==>
>sort -t/ -n -k3.1,3.4 -k2 -k1 myfile.txt
>
>Xicheng
Don't believe in being prepared for the Y10K problem, eh? *grin*
Note: your original works _just_fine_, because of the '-n' flag that precedes
all the keys.
| |
| Xicheng 2006-03-17, 2:57 am |
| Robert Bonomi wrote:
> In article <1142434424.518800.83510@u72g2000cwu.googlegroups.com>,
> Xicheng <xicheng@gmail.com> wrote:
>
> Don't believe in being prepared for the Y10K problem, eh? *grin*
not really, at least in my Ubuntu-box, given the following data, I got
different results by using two different ways I have two numbers
behind the comma in line-5, and line-7):
===================
1/11/2002,filed2,filed3
2/14/2005,dsgdgsd
8/31/2001,ewtrgtd
1/3/1999,sgdf
2/7/2005,1sdf
2/2/2005,eraef
1/7/2005,2eraef
====================
sort -t/ -n -k3.1,3.4 -k1 -k2 myfile.txt
====print===========
1/3/1999,sgdf
8/31/2001,ewtrgtd
1/11/2002,filed2,filed3
1/7/2005,2eraef
2/2/2005,eraef
2/7/2005,1sdf
2/14/2005,dsgdgsd
==================
sort -t/ -n -k3 -k1 -k2 myfile.txt
=====print==========
1/3/1999,sgdf
8/31/2001,ewtrgtd
1/11/2002,filed2,filed3
2/2/2005,eraef
2/14/2005,dsgdgsd
2/7/2005,1sdf
1/7/2005,2eraef
===================
Xicheng
> Note: your original works _just_fine_, because of the '-n' flag that precedes
> all the keys.
|
|
|
|
|