glly
glly

Reputation: 107

AWK - Using Variables in shell

I have an input file like:

INFO  2016-06-15 00:10:43.173WMSPackingOrderManager    llocatePackingOrderToWorkplaceBoxed -Remaining capacity of workplace WMSPackingWorkplace[428654,PAC_MB_ 104] is -43
INFO  2016-06-15 00:30:43.173WMSPackingOrderManager    llocatePackingOrderToWorkplaceBoxed -Remaining capacity of workplace WMSPackingWorkplace[428654,PAC_MB_ 104] is -43
INFO  2016-06-15 00:30:43.173WMSPackingOrderManager    llocatePackingOrderToWorkplaceBoxed -Remaining capacity of workplace WMSPackingWorkplace[428654,PAC_MB_ 104] is -43
INFO  2016-06-15 00:30:43.173WMSPackingOrderManager    llocatePackingOrderToWorkplaceBoxed -Remaining capacity of workplace WMSPackingWorkplace[428654,PAC_MB_ 104] is -43
INFO  2016-06-15 00:30:43.173WMSPackingOrderManager    llocatePackingOrderToWorkplaceBoxed -Remaining capacity of workplace WMSPackingWorkplace[428654,PAC_MB_ 104] is -43

The code I currently have is as follows:

cat inputFile.log | awk -v date="2016-06-15" -v startTime="00:10" -v endTime="00:20" '$2" "$3>=from&&$2" "$3<=to' from=date" "startTime to=date" "endTime

I am trying to get the from and to to use the variables provided but to no luck. I have tried escaping the special characters with \ but this didn't seem to effect it.

Upvotes: 1

Views: 79

Answers (1)

kemiya
kemiya

Reputation: 470

You can try this

cat inputFile.log | awk -v date="2016-06-15" -v startTime="00:10" -v endTime="00:20" 'BEGIN{from=date" "startTime; to=date" "endTime}($2" "$3)>=from && ($2" "$3)<=to'

or

awk -v date="2016-06-15" -v startTime="00:10" -v endTime="00:20" 'BEGIN{from=date" "startTime; to=date" "endTime}($2" "$3)>=from && ($2" "$3)<=to' inputFile.log

without use cat. Thanks to @Ed Morton

Output

INFO  2016-06-15 00:10:43.173WMSPackingOrderManager    llocatePackingOrderToWorkplaceBoxed -Remaining capacity of workplace WMSPackingWorkplace[428654,PAC_MB_ 104] is -43

Upvotes: 3

Related Questions