|
|
|
|
||||||
| comp.unix.shell Using and programming the Unix shell. |
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
To all
How to sort field with HH:MM format directly ? I need to using cat sort.txt|tr ':' '.' | sort -r -n -k 3 | tr '.' ':' data file annac pts/85 1:42 1056932 201 annay2 pts/94 1:31 1048696 233 anny pts/106 1:05 3985530 335 antheaz pts/313 2:16 7438440 134 aprilc pts/367 1:28 3567818 243 barrys pts/536 0:51 3305706 401 beatrip pts/697 0:47 4669568 423 bellaf pts/514 1:20 5283892 270 benq pts/720 1:55 3801242 169 berryl pts/229 1:11 1360096 307 bettyc pts/99 1:14 7086158 291 bettyk pts/145 0:50 6508572 406 Incorrect sorting cat sort.txt|sort -r -n -k 3 antheaz pts/313 2:16 7438440 134 bettyc pts/99 1:14 7086158 291 berryl pts/229 1:11 1360096 307 benq pts/720 1:55 3801242 169 bellaf pts/514 1:20 5283892 270 aprilc pts/367 1:28 3567818 243 anny pts/106 1:05 3985530 335 annay2 pts/94 1:31 1048696 233 annac pts/85 1:42 1056932 201 bettyk pts/145 0:50 6508572 406 beatrip pts/697 0:47 4669568 423 barrys pts/536 0:51 3305706 401 Correct sorting cat sort.txt|tr ':' '.' | sort -r -n -k 3 | tr '.' ':' antheaz pts/313 2:16 7438440 134 benq pts/720 1:55 3801242 169 annac pts/85 1:42 1056932 201 annay2 pts/94 1:31 1048696 233 aprilc pts/367 1:28 3567818 243 bellaf pts/514 1:20 5283892 270 bettyc pts/99 1:14 7086158 291 berryl pts/229 1:11 1360096 307 anny pts/106 1:05 3985530 335 barrys pts/536 0:51 3305706 401 bettyk pts/145 0:50 6508572 406 beatrip pts/697 0:47 4669568 423 moonhk |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
moonhk <moon_ils-se@yahoo.com.hk> wrote:
> To all > > How to sort field with HH:MM format directly ? > > I need to using cat sort.txt|tr ':' '.' | sort -r -n -k 3 | tr '.' ':' > > data file > annac pts/85 1:42 1056932 201 > annay2 pts/94 1:31 1048696 233 > anny pts/106 1:05 3985530 335 > antheaz pts/313 2:16 7438440 134 > aprilc pts/367 1:28 3567818 243 > barrys pts/536 0:51 3305706 401 > beatrip pts/697 0:47 4669568 423 > bellaf pts/514 1:20 5283892 270 > benq pts/720 1:55 3801242 169 > berryl pts/229 1:11 1360096 307 > bettyc pts/99 1:14 7086158 291 > bettyk pts/145 0:50 6508572 406 > > Incorrect sorting > cat sort.txt|sort -r -n -k 3 > antheaz pts/313 2:16 7438440 134 > bettyc pts/99 1:14 7086158 291 > berryl pts/229 1:11 1360096 307 > benq pts/720 1:55 3801242 169 > bellaf pts/514 1:20 5283892 270 > aprilc pts/367 1:28 3567818 243 > anny pts/106 1:05 3985530 335 > annay2 pts/94 1:31 1048696 233 > annac pts/85 1:42 1056932 201 > bettyk pts/145 0:50 6508572 406 > beatrip pts/697 0:47 4669568 423 > barrys pts/536 0:51 3305706 401 > > Correct sorting > cat sort.txt|tr ':' '.' | sort -r -n -k 3 | tr '.' ':' > antheaz pts/313 2:16 7438440 134 > benq pts/720 1:55 3801242 169 > annac pts/85 1:42 1056932 201 > annay2 pts/94 1:31 1048696 233 > aprilc pts/367 1:28 3567818 243 > bellaf pts/514 1:20 5283892 270 > bettyc pts/99 1:14 7086158 291 > berryl pts/229 1:11 1360096 307 > anny pts/106 1:05 3985530 335 > barrys pts/536 0:51 3305706 401 > bettyk pts/145 0:50 6508572 406 > beatrip pts/697 0:47 4669568 423 You should avoid using cat just to dump the contents of a file through a pipe to the standard input of a command. It is not required and is generally a bad habit. If you need to feed the contents of a file to the standard input of a command, use input redirection. This means, in the above case of tr, you would type: tr ':' '.' <sort.txt To solve your problem with a single command, type: sort -r -b -k 3 sort.txt Notice that you don't need to use input redirection in this case, since sort accepts the input file name as an argument. -- Kenan Kalajdzic |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
moonhk <moon_ils-se@yahoo.com.hk> wrote:
> To all > > How to sort field with HH:MM format directly ? > > I need to using cat sort.txt|tr ':' '.' | sort -r -n -k 3 | tr '.' ':' > > data file > annac pts/85 1:42 1056932 201 > annay2 pts/94 1:31 1048696 233 > anny pts/106 1:05 3985530 335 > antheaz pts/313 2:16 7438440 134 > aprilc pts/367 1:28 3567818 243 > barrys pts/536 0:51 3305706 401 > beatrip pts/697 0:47 4669568 423 > bellaf pts/514 1:20 5283892 270 > benq pts/720 1:55 3801242 169 > berryl pts/229 1:11 1360096 307 > bettyc pts/99 1:14 7086158 291 > bettyk pts/145 0:50 6508572 406 > > Incorrect sorting > cat sort.txt|sort -r -n -k 3 > antheaz pts/313 2:16 7438440 134 > bettyc pts/99 1:14 7086158 291 > berryl pts/229 1:11 1360096 307 > benq pts/720 1:55 3801242 169 > bellaf pts/514 1:20 5283892 270 > aprilc pts/367 1:28 3567818 243 > anny pts/106 1:05 3985530 335 > annay2 pts/94 1:31 1048696 233 > annac pts/85 1:42 1056932 201 > bettyk pts/145 0:50 6508572 406 > beatrip pts/697 0:47 4669568 423 > barrys pts/536 0:51 3305706 401 > > Correct sorting > cat sort.txt|tr ':' '.' | sort -r -n -k 3 | tr '.' ':' > antheaz pts/313 2:16 7438440 134 > benq pts/720 1:55 3801242 169 > annac pts/85 1:42 1056932 201 > annay2 pts/94 1:31 1048696 233 > aprilc pts/367 1:28 3567818 243 > bellaf pts/514 1:20 5283892 270 > bettyc pts/99 1:14 7086158 291 > berryl pts/229 1:11 1360096 307 > anny pts/106 1:05 3985530 335 > barrys pts/536 0:51 3305706 401 > bettyk pts/145 0:50 6508572 406 > beatrip pts/697 0:47 4669568 423 You should avoid using cat just to dump the contents of a file through a pipe to the standard input of a command. It is not required and is generally a bad habit. If you need to feed the contents of a file to the standard input of a command, use input redirection. This means, in the above case of tr, you would type: tr ':' '.' <sort.txt To solve your problem with a single command, type: sort -r -b -k 3 sort.txt Notice that you don't need to use input redirection in this case, since sort accepts the input file name as an argument. -- Kenan Kalajdzic |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
"moonhk" <moon_ils-se@yahoo.com.hk> wrote in message news:1188898378.477846.237140@y42g2000hsy.googlegr oups.com... > To all > > How to sort field with HH:MM format directly ? > > > I need to using cat sort.txt|tr ':' '.' | sort -r -n -k 3 | tr '.' ':' > > data file > annac pts/85 1:42 1056932 201 > annay2 pts/94 1:31 1048696 233 > anny pts/106 1:05 3985530 335 That is probably as good a way as any: pre-process the data to make the sort easy (in this case by allowing you to treat the time as a number), then sort it, then change the data back. There are different ways of doing this but none obviously better. You say the data is HH:MM but you show only H:MM. If the data is tab-separated and the hour field space-padded (so that : is always the third character in the time field) then you can do it in one sort command by specifying character positions in sort fields to separate hours and minutes. You could use perl or another scripting language with your own comparison function but why bother? If you are really determined, you could implement your own locale where : is treated as a decimal point. It might be easier just to write your own sort command (as above or by hacking Gnu sort). Aside from minor stylistic differences like not using cat and specifying the end of the sort field (sort -k3,3nr) I'd do it your way. -- John. |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
On Tue, 04 Sep 2007 02:32:58 -0700, moonhk
<moon_ils-se@yahoo.com.hk> wrote: > > > To all > > How to sort field with HH:MM format directly ? > > > I need to using cat sort.txt|tr ':' '.' | sort -r -n -k 3 | tr '.' ':' > tr will change all : to . and all . to :. You could use awk to change only field 3, or add a leading zero when the hour is less than 10 (then character sorting would have the same result as numeric sorting). > data file > annac pts/85 1:42 1056932 201 > annay2 pts/94 1:31 1048696 233 > anny pts/106 1:05 3985530 335 > antheaz pts/313 2:16 7438440 134 > aprilc pts/367 1:28 3567818 243 > barrys pts/536 0:51 3305706 401 > beatrip pts/697 0:47 4669568 423 > bellaf pts/514 1:20 5283892 270 > benq pts/720 1:55 3801242 169 > berryl pts/229 1:11 1360096 307 > bettyc pts/99 1:14 7086158 291 > bettyk pts/145 0:50 6508572 406 > > Incorrect sorting > cat sort.txt|sort -r -n -k 3 > antheaz pts/313 2:16 7438440 134 > bettyc pts/99 1:14 7086158 291 > berryl pts/229 1:11 1360096 307 > benq pts/720 1:55 3801242 169 > bellaf pts/514 1:20 5283892 270 > aprilc pts/367 1:28 3567818 243 > anny pts/106 1:05 3985530 335 > annay2 pts/94 1:31 1048696 233 > annac pts/85 1:42 1056932 201 > bettyk pts/145 0:50 6508572 406 > beatrip pts/697 0:47 4669568 423 > barrys pts/536 0:51 3305706 401 > > Correct sorting > cat sort.txt|tr ':' '.' | sort -r -n -k 3 | tr '.' ':' > antheaz pts/313 2:16 7438440 134 > benq pts/720 1:55 3801242 169 > annac pts/85 1:42 1056932 201 > annay2 pts/94 1:31 1048696 233 > aprilc pts/367 1:28 3567818 243 > bellaf pts/514 1:20 5283892 270 > bettyc pts/99 1:14 7086158 291 > berryl pts/229 1:11 1360096 307 > anny pts/106 1:05 3985530 335 > barrys pts/536 0:51 3305706 401 > bettyk pts/145 0:50 6508572 406 > beatrip pts/697 0:47 4669568 423 > > > > > > moonhk > -- Life is a game. Money is how we keep score. -- Ted Turner |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
On 9 5 , 3 04 , Bill Marcum <marcumb...@bellsouth.net> wrote:
> On Tue, 04 Sep 2007 02:32:58 -0700, moonhk <moon_ils...@yahoo.com.hk> wrote: > > > To all > > > How to sort field with HH:MM format directly ? > > > I need to using cat sort.txt|tr ':' '.' | sort -r -n -k 3 | tr '.' ':' > > tr will change all : to . and all . to :. You could use awk to change > only field 3, or add a leading zero when the hour is less than 10 (then > character sorting would have the same result as numeric sorting). > > > > > > > data file > > annac pts/85 1:42 1056932 201 > > annay2 pts/94 1:31 1048696 233 > > anny pts/106 1:05 3985530 335 > > antheaz pts/313 2:16 7438440 134 > > aprilc pts/367 1:28 3567818 243 > > barrys pts/536 0:51 3305706 401 > > beatrip pts/697 0:47 4669568 423 > > bellaf pts/514 1:20 5283892 270 > > benq pts/720 1:55 3801242 169 > > berryl pts/229 1:11 1360096 307 > > bettyc pts/99 1:14 7086158 291 > > bettyk pts/145 0:50 6508572 406 > > > Incorrect sorting > > cat sort.txt|sort -r -n -k 3 > > antheaz pts/313 2:16 7438440 134 > > bettyc pts/99 1:14 7086158 291 > > berryl pts/229 1:11 1360096 307 > > benq pts/720 1:55 3801242 169 > > bellaf pts/514 1:20 5283892 270 > > aprilc pts/367 1:28 3567818 243 > > anny pts/106 1:05 3985530 335 > > annay2 pts/94 1:31 1048696 233 > > annac pts/85 1:42 1056932 201 > > bettyk pts/145 0:50 6508572 406 > > beatrip pts/697 0:47 4669568 423 > > barrys pts/536 0:51 3305706 401 > > > Correct sorting > > cat sort.txt|tr ':' '.' | sort -r -n -k 3 | tr '.' ':' > > antheaz pts/313 2:16 7438440 134 > > benq pts/720 1:55 3801242 169 > > annac pts/85 1:42 1056932 201 > > annay2 pts/94 1:31 1048696 233 > > aprilc pts/367 1:28 3567818 243 > > bellaf pts/514 1:20 5283892 270 > > bettyc pts/99 1:14 7086158 291 > > berryl pts/229 1:11 1360096 307 > > anny pts/106 1:05 3985530 335 > > barrys pts/536 0:51 3305706 401 > > bettyk pts/145 0:50 6508572 406 > > beatrip pts/697 0:47 4669568 423 > > > moonhk > > -- > Life is a game. Money is how we keep score. > -- Ted Turner- - > > - - I try sort without using cat, the result is not ok . It can not able to sort with Time. Just for number. sort -n -r -k 3 sort.txt anny pts/106 11:05 3985530 335 antheaz pts/313 2:16 7438440 134 bettyc pts/99 1:14 7086158 291 berryl pts/229 1:11 1360096 307 benq pts/720 1:55 3801242 169 bellaf pts/514 1:20 5283892 270 aprilc pts/367 1:28 3567818 243 annay2 pts/94 1:31 1048696 233 annac pts/85 1:42 1056932 201 bettyk pts/145 0:50 6508572 406 beatrip pts/697 0:47 4669568 423 barrys pts/536 0:51 3305706 401 |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
"moonhk" <moon_ils-se@yahoo.com.hk> wrote in message news:1188955488.326531.81320@o80g2000hse.googlegro ups.com... > I try sort without using cat, the result is not ok . It can not able > to sort with Time. Just for number. > > sort -n -r -k 3 sort.txt > anny pts/106 11:05 3985530 335 > antheaz pts/313 2:16 7438440 134 > bettyc pts/99 1:14 7086158 291 > berryl pts/229 1:11 1360096 307 > benq pts/720 1:55 3801242 169 > bellaf pts/514 1:20 5283892 270 > aprilc pts/367 1:28 3567818 243 > annay2 pts/94 1:31 1048696 233 > annac pts/85 1:42 1056932 201 > bettyk pts/145 0:50 6508572 406 > beatrip pts/697 0:47 4669568 423 > barrys pts/536 0:51 3305706 401 > OK, now you have given us an example with a double-figure hour (11:05) we can see it is space-padded so, if we set the field-separator to a character that is not in the file then the whole line is the first (and only) field, with the : at character position 24, so the hour is positions 22-23, and the minutes at 25-26. sort -t, -k1.22,1.23nr -k1.25,1.26nr filename but you would want to document that you are sorting by time because it is not obvious, and perhaps incorporate a check that the layout of the data does not change by a character or two. -- John. |
|
|
|
#8 |
|
Messages: n/a
Hébergeur: |
On 9 5 , 4 22 , "John L" <j...@lammtarra.notthisbit.fslife.co.uk>
wrote: > "moonhk" <moon_ils...@yahoo.com.hk> wrote in messagenews:1188955488.326531.81320@o80g2000hse.go oglegroups.com... > > I try sort without using cat, the result is not ok . It can not able > > to sort with Time. Just for number. > > > sort -n -r -k 3 sort.txt > > anny pts/106 11:05 3985530 335 > > antheaz pts/313 2:16 7438440 134 > > bettyc pts/99 1:14 7086158 291 > > berryl pts/229 1:11 1360096 307 > > benq pts/720 1:55 3801242 169 > > bellaf pts/514 1:20 5283892 270 > > aprilc pts/367 1:28 3567818 243 > > annay2 pts/94 1:31 1048696 233 > > annac pts/85 1:42 1056932 201 > > bettyk pts/145 0:50 6508572 406 > > beatrip pts/697 0:47 4669568 423 > > barrys pts/536 0:51 3305706 401 > > OK, now you have given us an example with a double-figure > hour (11:05) we can see it is space-padded so, if we set > the field-separator to a character that is not in the file then > the whole line is the first (and only) field, with the : at > character position 24, so the hour is positions 22-23, and > the minutes at 25-26. > > sort -t, -k1.22,1.23nr -k1.25,1.26nr filename > > but you would want to document that you are sorting by > time because it is not obvious, and perhaps incorporate > a check that the layout of the data does not change by > a character or two. > > -- > John.- - > > - - Thank. It works. Now, I am know how to using -k parameter. sort -t, -k1.22,1.23nr -k1.25,1.26nr sort.txt knny pts/6 11:11 985530 335 anny pts/106 11:05 3985530 335 antheaz pts/313 2:16 7438440 134 benq pts/720 1:55 3801242 169 annac pts/85 1:42 1056932 201 annay2 pts/94 1:31 1048696 233 aprilc pts/367 1:28 3567818 243 bellaf pts/514 1:20 5283892 270 bettyc pts/99 1:14 7086158 291 berryl pts/229 1:11 1360096 307 barrys pts/536 0:51 3305706 401 bettyk pts/145 0:50 6508572 406 beatrip pts/697 0:47 4669568 423 |
|
![]() |
| Outils de la discussion | |
|
|