lyoncc
February 13, 2009, 1:53pm
1
Hi all.
HPUX - posix shell - script question
Here's my data:
f1 f2 f3 f4 f5 f6
|02/12/09|12:33PM|3|Oceanview |OVT #1| VISA/MC
|02/12/09|12:14PM|3|Oceanview |OVT #1| VISA/MC
|02/12/09|09:13AM|4|Oceanview |OVT #1| VISA/MC
|02/12/09|12:40PM|3|Oceanview |OVT #1| VISA/MC
|02/12/09|01:11PM|3|Oceanview |OVT #2| VISA/MC
|02/12/09|01:08AM|3|Oceanview |OVT #2| VISA/MC
|02/12/09|02:45PM|3|Oceanview |OVT #2| VISA/MC
I need to keep the data sorted by f5..how can I sub sort this by time (f2)?
TIA!!!!!
sort -t\| -k5 -k3.6 -k3.1n
lyoncc
February 18, 2009, 12:31pm
3
the -k5 and -k3.6 work perfectly.
not sure how the -k3.1n is going to work in that the "first" time would be 12:00AM and the "last" time would be 12:59PM. Doesn't -k3.1n mean first character 0 to 9?
TIA!!!!
lyoncc
February 18, 2009, 12:34pm
4
I mean "last" time would be 11:59PM.
In cases like these, you'll need to compute a sort-key field then sort by that.
Something like this:
cat << EOF |
|02/12/09|12:33PM|3|Oceanview |OVT #1| VISA/MC
|02/12/09|12:14PM|3|Oceanview |OVT #1| VISA/MC
|02/12/09|09:13AM|4|Oceanview |OVT #1| VISA/MC
|02/12/09|12:40PM|3|Oceanview |OVT #1| VISA/MC
|02/12/09|01:11PM|3|Oceanview |OVT #2| VISA/MC
|02/12/09|01:08AM|3|Oceanview |OVT #2| VISA/MC
|02/12/09|02:45PM|3|Oceanview |OVT #2| VISA/MC
EOF
nawk -F\| '
function time_convert()
{
hours = substr( $3, 1, 2 );
hours *= 60;
minutes = substr( $3, 4, 2 );
meridian = substr( $3, 6, 1 );
if ( meridan == "P" ){
hours += 12 * 60;
}
time_key = hours + minutes;
return;
}
{
time_convert();
print time_key, $0;
}' |
sort -n |
cut -d\| -f2-
btw -- cj -- nice solution! i didn't notice (duh) that you could sort on the A then the P.
lyoncc
February 24, 2009, 6:45pm
7
|02/23/09|06:56PM|3|Plaza Cafe|Plaza2| | |VISA/MC Student | | |Y| | 7.50
|02/23/09|07:05PM|3|C/Ventana |Vent#2| | |VISA/MC Student | | |Y| | 15.77
|02/23/09|07:07PM|3|Plaza Cafe|Plaza2| | |VISA/MC Staff | | |Y| | 7.49
|02/23/09|07:15PM|3|FoodWorx10|Worx#2| | |VISA/MC Student | | |Y| | 4.50
|02/23/09|07:18PM|3|SierraSum |West 2| | |VISA/MC Student | | |Y| | 11.90
|02/23/09|07:28PM|1|Earl's |E/PSou| | |VISA/MC Student | | |Y| | 2.84
|02/23/09|07:42PM|4|Oceanview |OVT #1| | |VISA/MC Student | | |Y| | 12.20
sort -t"|" -k6 test.data > test.data1
|02/23/09|07:28PM|1|Earl's |E/PSou| | |VISA/MC Student | | |Y| | 2.84
|02/23/09|07:42PM|4|Oceanview |OVT #1| | |VISA/MC Student | | |Y| | 12.20
|02/23/09|07:07PM|3|Plaza Cafe|Plaza2| | |VISA/MC Staff | | |Y| | 7.49
|02/23/09|06:56PM|3|Plaza Cafe|Plaza2| | |VISA/MC Student | | |Y| | 7.50
|02/23/09|07:05PM|3|C/Ventana |Vent#2| | |VISA/MC Student | | |Y| | 15.77
|02/23/09|07:18PM|3|SierraSum |West 2| | |VISA/MC Student | | |Y| | 11.90
|02/23/09|07:15PM|3|FoodWorx10|Worx#2| | |VISA/MC Student | | |Y| | 4.50
perfect, now add second key
sort -t "|" -k6 -k3n test.data > test.data2
|02/23/09|07:28PM|1|Earl's |E/PSou| | |VISA/MC Student | | |Y| | 2.84
|02/23/09|07:42PM|4|Oceanview |OVT #1| | |VISA/MC Student | | |Y| | 12.20
|02/23/09|07:07PM|3|Plaza Cafe|Plaza2| | |VISA/MC Staff | | |Y| | 7.49
|02/23/09|06:56PM|3|Plaza Cafe|Plaza2| | |VISA/MC Student | | |Y| | 7.50
|02/23/09|07:05PM|3|C/Ventana |Vent#2| | |VISA/MC Student | | |Y| | 15.77
|02/23/09|07:18PM|3|SierraSum |West 2| | |VISA/MC Student | | |Y| | 11.90
|02/23/09|07:15PM|3|FoodWorx10|Worx#2| | |VISA/MC Student | | |Y| | 4.50
Doesn't seem to be responding to -k3...I've tried -k3 -k3n -k3.1,2 -k3.1,2n
TIA!!!
sort -t "|" -k6,6 -k3n test.data > test.data2
#!/usr/bin/perl
use strict;
my %hash;
open FH,"<a";
while(<FH>){
my @tmp=split("[|]",$_);
my $key=$tmp[5].substr($tmp[2],5,2).substr($tmp[2],0,5);
$hash{$key}=$_;
}
close FH;
map { print $hash{$_} } sort keys %hash;