 |
|
 |
|
|
 |
Simple but challenging sort problem |
 |
 |
|
|
03-15-06 12:49 PM
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.
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
|
 |
Re: Simple but challenging sort problem |
 |
 |
|
|
03-15-06 12:49 PM
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
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
|
 |
Re: Simple but challenging sort problem |
 |
 |
|
|
03-15-06 12:49 PM
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
[ Post a follow-up to this message ]
|
|
|
 |
|
|
|
 |
Re: Simple but challenging sort problem |
 |
 |
|
|
03-15-06 12:49 PM
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
[ Post a follow-up to this message ]
|
|
|
 |
|
|
|
 |
Re: Simple but challenging sort problem |
 |
 |
|
|
03-15-06 12:49 PM
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
[ Post a follow-up to this message ]
|
|
|
 |
|
|
|
 |
Re: Simple but challenging sort problem |
 |
 |
|
|
03-15-06 10: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
[ Post a follow-up to this message ]
|
|
|
 |
|
|
|
 |
Re: Simple but challenging sort problem |
 |
 |
|
|
03-15-06 10: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.
>
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
|
 |
Re: Simple but challenging sort problem |
 |
 |
|
|
03-16-06 01:48 AM
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.c
om ***
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
|
 |
Re: Simple but challenging sort problem |
 |
 |
|
|
03-16-06 10: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 precede
s
all the keys.
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
|
 |
Re: Simple but challenging sort problem |
 |
 |
|
|
03-17-06 07: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 prece
des
> all the keys.
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
|
|
Sponsored Links |
 |
 |
|
|
 |
All times are GMT. The time now is 03:31 AM. |
 |
|
|
 |
|
 |
|
|
 |
|
Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
|
|
|
|
Medical and Health forum | Computer Games Reviews | Graphics design forum
|
 |
|
 |
|