AWK - 在shell中使用变量

时间:2016-06-22 11:05:45

标签: awk

我有一个输入文件,如:

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

我目前的代码如下:

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

我试图让fromto使用提供的变量,但没有运气。我尝试使用\转义特殊字符,但这似乎没有影响它。

1 个答案:

答案 0 :(得分:3)

你可以试试这个

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'

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

不使用cat。感谢@Ed Morton

<强>输出

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