Saturday, May 30, 2009

awk example using arrays, the split() function, and regex

#record format

Request: 10
As Of: 05/27/2009 1:34:58 pm
Irrevelant Data ...
Summary: Virus on PC.
Description: Virus on PC.

Request: 11
As Of: 05/27/2009 1:34:58 pm
Irrevelant Data...
Summary: room1 several computers in a row are freezing up and student...
Description: room1 several computers in a row are freezing up and students have to reboot.


awk -v pat1="room1" -v pat2="freezing|crash" 'BEGIN { RS=""; FS="Summary"; i=0; j=0; k=0 } {\
i=i+1; \
field1=$1; \
split(field1,sub_fields,":"); \
request=sub_fields[1]; \
the_date=sub_fields[4]; \
if ($2 ~ pat1){ \
k=k+1; \
if ($2 ~ pat2){ \
j=1+j; \
print "***Record***", request, the_date; print " ---Start Data---", $2; } } \
} END { print "Processed, ", i, ",num of lab matches,", k,", records num of keywords matches: ,", j }' requests


#Output

***Record*** Request 05/27/2009 1
---Start Data--- : room1 several computers in a row are freezing up and student...
Description: room1 several computers in a row are freezing up and students have to reboot.
Processed, 2 ,num of lab matches, 1 , records num of keywords matches: , 1

No comments:

Post a Comment