restriced key sort in unix
I have a file file1.txt with data:
2010/09/04,21:53:42.048,a
2010/09/04,21:53:40.923,b
2010/09/04,21:53:40.923,a
2010/09/04,21:50:42.048,a.
I want to sort the file based on time stamp. I am currently using
sort -t% -k1.1,1.23 file1.txt > file2.txt
My expected output is
2010/09/04,21:50:42.048,a
2开发者_运维技巧010/09/04,21:53:40.923,b
2010/09/04,21:53:40.923,a
2010/09/04,21:53:42.048,a
But, I am getting the following output
2010/09/04,21:50:42.048,a
2010/09/04,21:53:40.923,a
2010/09/04,21:53:40.923,b
2010/09/04,21:53:42.048,a
I am using SFU3.5 for windows. My sort usage is
usage: sort [-o output] [-cmubdfinr] [-t char] [-T char] [-k keydef] ... [files]
Please provide possible soultion.
Looks like a stability problem, so try to use the --stable
option:
-s, --stable
stabilize sort by disabling last-resort comparison
Update:
It seems that SFU does not support the -s
option and is unstable all the time. You can try to use an alternative sort, like the one shipped with unxutils.
If you can't get a sort with a "stable" option then you can make an extra key with "cat -n", or awk, use that in the sort when you need the original order, and strip it off later. Why not tell sort that comma is the field separator too, then you don't need to count 23 characters.
awk '{print NR "," $0}' dat | sort -t, -k2,2 -k1n,1 | sed 's/^[^,]*,//'
A stable sort is one that preserves the original order if the sort keys are equal.
精彩评论