0

I have below test data and I have been able to get data for current date, How can I compare time to get me data from 00:00:01 to 08:00:00?

Test1   prog  02/20/2017  03:45:56 Success
Test2   prog  02/20/2017  05:21:38 Fail
Test3   prog  02/20/2017  09:35:36 Success
Test4   prog  02/19/2017  06:15:56 Fail
Test5   prog  02/18/2017  07:35:16 Active
Test6   prog  02/19/2017  03:45:56 Success

I did

a=`date +%m/%d/%Y`
awk -v a="$a" '$3==a' file

I have no thoughts on how do I break fourth column i.e. time to compare it for time range.

pii_ke
  • 2,811
  • 2
  • 20
  • 30
Sid
  • 161
  • 1
  • 10

2 Answers2

0
a=$(date +%m/%d/%Y)

awk -F":| *" -v a="$a" '$3==a {t=($4*60*60 + $5*60 + $6); if(t>=1 && t<=28800) print}' File

Use : and space sequence as field seperators. Here, I am converting time to seconds and checking if it's between (inclusive) 1s and 8hrs. If then, print the line. Date check is done as the first filter ($3==a)

Arjun Mathew Dan
  • 5,240
  • 1
  • 16
  • 27
  • Is it possible to sort also on this time column? – Sid Feb 23 '17 at 12:30
  • Is it possible if date column is varying, as in it searches a=$(date +%m/%d/%Y) from the beginning of the line and when found then calculate time from next field – Sid Mar 01 '17 at 10:14
  • @Sid: Like this: `awk -F":| *" -v a="$a" '{i=0; while(++i<=NF){if(match($i, a)){ t=($(i+1)*3600 + $(i+2)*60 + $(i+3)); if(t>=1 && t<=28800) print}}}' File` – Arjun Mathew Dan Mar 01 '17 at 10:30
0
awk -v a=`date '+%m/%d/%Y'` '$3$4 ~ a"0[0-8]"' file

The above gives required lines for today's date.

If you want all such lines, then use:

awk '$4 ~ "0[0-8]"' file

Note that you have to modify the regular expression (the string after ~) according to your time range.

pii_ke
  • 2,811
  • 2
  • 20
  • 30