0

I want to display the events in a file after a certain time. I have a bash shell variable $DateMin as shown below that I am passing to awk:

echo | awk -v Date="$DateMin" '{print Date}'

2023-04-28 11:2

The following works where I specify the same string that is in the variable:

tac file.log | awk -v Date="$DateMin" '!flag; /2023-04-28 11:2/{flag = 1};' | tac

But when I try to used the Date variable, it doesn't work. It appears to show the entire file:

tac file.log | awk -v Date="$DateMin" '!flag; /Date/{flag = 1};' | tac

Any ideas of how to make this work?

WesR
  • 11
  • 4
  • please update the question with a (small) sample input file – markp-fuso Apr 28 '23 at 14:07
  • if the file contains 2x rows ... `2023-04-28 11:20` and `2023-04-28 11:25` .... this code will only print the `2023-04-28 11:25` line; is that what you're expecting? – markp-fuso Apr 28 '23 at 14:19

1 Answers1

0

You can't use a variable in /.../. You need:

awk -v var="$date" '$0 ~ var'

~ is the regex match operator.

Gilles Quénot
  • 173,512
  • 41
  • 224
  • 223
  • I'm sure your right, but I don't understand your response enough to make it useful. Do you have an example of how to accomplish my objective with this information? – WesR Apr 28 '23 at 13:58
  • **Test it** instead of `/Date/` use `'$0 ~ date'` – Gilles Quénot Apr 28 '23 at 14:02
  • I tried this, but it it shows the events from the start of the file: `tac file.log | awk -v Date="$DateMin" '!flag; /$0 ~ Date/ {flag=1};'|tac` – WesR Apr 28 '23 at 14:06
  • Oh! Got it. Thanks. `tac file.log | awk -v Date="$DateMin" '!flag; $0 ~ Date {flag=1};'|tac` – WesR Apr 28 '23 at 14:11