1834828 Members
2098 Online
110070 Solutions
New Discussion

Re: orders

 
SAM_24
Frequent Advisor

orders

Hi,

I have plain text file which has business orders with time stamp. I want to grep orders for every five minutes starting from 12PM-4PM. Time format is like HH:MM:SS.Please help.

Thanks.
Never quit
5 REPLIES 5
curt larson_1
Honored Contributor

Re: orders

this will give you the lines with a time between, 0-4, 5-9, 10-14, increments.

awk -v hour=$hour -v min-$min '
/[0-9]{1,2}:[0-9]{1,2}:[0-9]{1,2}/ {

for ( i=1,i<=NF;i++ ) {
if ( $i ~ /[0-9]{1,2}:[0-9]{1,2}:[0-9]{1,2}/ ) date = $i;
}
split(date,a,":");
if ( hour == a[1] ) {
m1=sprintf("%d",a[2]/5;
m2=sprintf("%d",a[2]/5;
if ( m1 == m2 ) print $0;
}}'

then it is pretty easy to loop through hours and minutes
ehour=5
hour=12
min=0
while (( $hour < ehour ))
do
while (( $min < 60 ))
do

#do the above script

min=$(( $min + 5 ))
done

hour=$(( $hour + 1 ))
if (( $hour > 12 )) ;then
hour=$(( $hour - 12 ))
done

you'll have to decide how you handle AM/PM issues
curt larson_1
Honored Contributor

Re: orders

oops these both should be a[2]:

m1=sprintf("%d",a[2]/5;
m2=sprintf("%d",a[2]/5;

one should be min, as in:

m1=sprintf("%d",a[2]/5;
m2=sprintf("%d",min/5;
Nicolas Dumeige
Esteemed Contributor

Re: orders

Hello,

Not really sure what you want...
if you want to get all lines from a text file where the hours are 12,01,02,03,04 PM with regular minutes 00 to 59 ... :

grep '[12,01,02,03,04]:[0-5][0-9]:[0-5][0-9]' textfile

For the 5 minutes interval, the regular expression can be :
[05,10,15,20,25,30,35,40,45,50,55]

Hope that helps
All different, all Unix
Hein van den Heuvel
Honored Contributor

Re: orders

Hmm, your description is not very specific.
Like is that text file already sorted by time? What about am-pm, did you want to grep for a orders in a 5 minute block where that block in perhpas indicated in a variable?

Here is a perl (or awk) solution to group an assumed to be pre-sorted file in labeled 5-minute intervals.

usage: perl -n time-groups.pl < textfile
Where time-groups.pl contains:

if (/(12|01|02|03)(:\d+:)(\d+)/) {
$range = sprintf("%s%s%02d",$1,$2,5*int($3/5));
print "\n-- $range --\n" unless ($headers{$range}++);
print;
}

find line with 12,01,02 or 03 (remeber in $1) followed be a decimal between colons (remember in $2) followed by a decimal (in $3).
Calculate group header as the first 2 found strings for HR:MM: and 'floor' the seconds by fives. ( divide by factor, int, re-mutiply by factor).
Print header with group range unless already printed as indicated by a counter in hash array.
Print line (if is matched the range)


Sample input:

12:00:04 aap
02:09:40 een noot
02:09:41 twee noot
02:09:42 drie noot
03:59:59 mies
04:01:00 geen teun


Sample output:

-- 12:00:00 --
12:00:04 aap

-- 02:09:40 --
02:09:40 een noot
02:09:41 twee noot
02:09:42 drie noot

-- 03:59:55 --
03:59:59 mies



So now, what did you really want to see?
:-).

Hein.

Hein van den Heuvel
Honored Contributor

Re: orders

Here is an other cut at a possible solution:

usage: perl time-range-grep.pl hh:mm:ss [minutes] < textfile

Where time-range-grep.pl contains:

($h,$m,$s)=split(/:/,shift);
$window=shift;
$window=5 unless ($window);
$h=0 if ($h==12);
$start=$h*60+$m;
while (<>) {
if (/(\d+):(\d+):\d+/) {
$t = ($1==12)? $2 : 60*$1 + $2;
print if (($t >= $start) && ($t < $start+$window)) ;
}
}

parse first argument into hours,minute
parse optional second argument into window, default 5
calculate start minute for period
find line with timestamps
calculate to minutes, making an exception of hour=12
in range? then print.

Sample input:

12:00:04 aap
02:09:40 een noot
02:09:41 twee noot
02:09:42 drie noot
03:59:59 mies
04:01:00 geen teun


Sample output:

# perl x.p 02:09:40 < x
02:09:40 een noot
02:09:41 twee noot
02:09:42 drie noot
# perl x.p 02:03:40 < x
# perl x.p 02:03:40 10 < x
02:09:40 een noot
02:09:41 twee noot
02:09:42 drie noot