PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Forums Hébergement > Forum Serveur - Sécurité et techniques > comp.unix.shell > How to sort field with HH:MM format ?
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
comp.unix.shell Using and programming the Unix shell.

How to sort field with HH:MM format ?

Réponse
 
LinkBack Outils de la discussion
Vieux 04/09/2007, 10h32   #1
moonhk
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut How to sort field with HH:MM format ?

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

  Réponse avec citation
Vieux 04/09/2007, 11h09   #2
Kenan Kalajdzic
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to sort field with HH:MM format ?

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
  Réponse avec citation
Vieux 04/09/2007, 11h09   #3
Kenan Kalajdzic
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to sort field with HH:MM format ?

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
  Réponse avec citation
Vieux 04/09/2007, 19h08   #4
John L
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to sort field with HH:MM format ?


"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.


  Réponse avec citation
Vieux 04/09/2007, 20h04   #5
Bill Marcum
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to sort field with HH:MM format ?

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
  Réponse avec citation
Vieux 05/09/2007, 02h24   #6
moonhk
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to sort field with HH:MM format ?

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

  Réponse avec citation
Vieux 05/09/2007, 09h22   #7
John L
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to sort field with HH:MM format ?


"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.


  Réponse avec citation
Vieux 05/09/2007, 10h17   #8
moonhk
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to sort field with HH:MM format ?

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


  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 13h21.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,20350 seconds with 16 queries