Running Shell Commands in AWK

I am trying to work with a log file and I need to specify a date range. So far (before any processing), I convert the date / time string to a timestamp with date --date "monday" +%s.

Now I want to be able to iterate over each line in the file, but check if the date (in human readable format) is within the acceptable range. To do this, I would like to do something like the following:

echo `awk '{if(`date --date "$3 $4 $5 $6 $7" +%s` > $START && `date --date "" +%s` <= $END){/*processing code here*/}}' myfile`

I don’t even know if this is possible ... I tried many variations, plus I could not find anything clear / used on the Internet.

thank

Update:

An example of myfile is as follows. Its logging IP addresses and access time:

123.80.114.20      Sun May 01 11:52:28 GMT 2011
144.124.67.139     Sun May 01 16:11:31 GMT 2011
178.221.138.12     Mon May 02 08:59:23 GMT 2011
+3
source share
3 answers

, , , , .

, . % 02d, 2 . dateTime "." .. : : , .

YYYYMMDD , LT, GT, EQ .

echo "178.221.138.12     Mon May 02 08:59:23 GMT 2011" \
| awk 'BEGIN {
mons["Jan"]=1 ; mons["Feb"]=2; mons["Mar"]=3
mons["Apr"]=4 ; mons["May"]=5; mons["Jun"]=6
mons["Jul"]=7 ; mons["Aug"]=8; mons["Sep"]=9
mons["Oct"]=10 ; mons["Nov"]=11; mons["Dec"]=12
}
{ 
   # 178.221.138.12     Mon May 02 08:59:23 GMT 2011
   printf("dateTime=%04d%02d%02d%02d%02d%02d\n", 
       $NF, mons[$3], $4, substr($5,1,2), substr($5,4,2), substr($5,7,2) )
} ' -v StartTime=20110105235959

-v StartTime , ( ) starTime.

, .

+1

mktime() awk. - (. ). .

#!/bin/bash
# input format:
#(1                  2   3   4  5        6   7)
#123.80.114.20      Sun May 01 11:52:28 GMT 2011

awk -v startTime=1304252691 -v endTime=1306000000 '
BEGIN {
  mons["Jan"]=1 ; mons["Feb"]=2; mons["Mar"]=3
  mons["Apr"]=4 ; mons["May"]=5; mons["Jun"]=6
  mons["Jul"]=7 ; mons["Aug"]=8; mons["Sep"]=9
  mons["Oct"]=10 ; mons["Nov"]=11; mons["Dec"]=12;
}
{
  hmsSpaced=$5; gsub(":"," ",hmsSpaced); 
  timeInSec=mktime($7" "mons[$3]" "$4" "hmsSpaced); 
  if (timeInSec > startTime && timeInSec <= endTime) print $0
}' myfile

( .)

, mktime() , :

awk -v startTime=1304252691 -v endTime=1306000000 't=mktime($7" "$3" "$4" "$5); if (t > startTime && t <= endTime) print $0}' myfile
+1

I'm not sure about the format of the data you are parsing, but I know that you cannot use backreferences in single quotes. You will have to use double quotes. If there are too many quotes enclosed, and this confuses you, you can also just save the output of your command dateto a variable in advance.

0
source

All Articles