1

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.

1
  • 1
    no need to say cat file | awk '...' , since awk '...' file does the same. Commented Jun 22, 2016 at 11:22

1 Answer 1

3

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
Sign up to request clarification or add additional context in comments.

5 Comments

Legend. Works like a charm.
Can you explain how "$3>=from&&$2" "$3<=to' this works?
'$2" "$3>=from&&$2" "$3<=to' this code is just a string comparison and it will compare the string in alphabetic order, from and to was defined in the BEGIN scope, so from="2016-06-15 00:10" and to="2016-06-15 00:20".
Some white space and parens would clarify: (($2" "$3) >= from) && (($2" "$3) <= to). I'm upvoting the awk part but lose the UUOC.
@EdMorton Thanks for you advice!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.