Searching multiple patterns using awk

Hello,

I have the following input file:

qh1adm 20130710111201 : tp import all QH1 u6 -Dsourcesystems=BFI,EBJ 
qh1adm 20130711151154 : tp import all QH1 u6 -Dsourcesystems=BFI,EBJ 
qx1adm 20130711151154 : tp count QX1 u6 -Dsourcesystems=B17,E17,EE7 
qh1adm 20130711151155 : tp import all QH1 u6 -Dsourcesystems=E7B,B17 
qh2adm 20130711151156 : tp import all QH2 u6 -Dsourcesystems=E7B,B17 
emiadm 20130711151158 : tp count EMI u6 -Dsourcesystems=BFI,EBJ,EEI 
qh1adm 20130711151200 : tp import all QH1 u6 -Dsourcesystems=BFI,EBJ

In this case I want all the records starting from 20130711151154 and that have QH1 in them.

This should be the output:

qh1adm 20130711151154 : tp import all QH1 u6 -Dsourcesystems=BFI,EBJ
qh1adm 20130711151155 : tp import all QH1 u6 -Dsourcesystems=E7B,B17
qh1adm 20130711151200 : tp import all QH1 u6 -Dsourcesystems=BFI,EBJ

I have tried something like this:

awk '/'qh1adm'/ && /'$STOPMARK'/,0' logfile

Nothing seems to work though

where $STOPMARK is 20130711151154 . But I am not getting the correct output. Can you please assist? Thanks a lot.

awk '$2 >= 20130711151154 && /QH1/' file
1 Like

Please use code tags as required by forum rules!

You are using an address range with an "open" (unset) end, which should work fine given it will start, which it can't, as qh1adm is never found. Once it started, it won't stop until its ending condition is met, so you'd need an if clause in the action section. Sth like

awk "/$STOPMARK/,0 {if ($0 ~ /QH1/) print}" file

(untested! You may need to escape the slashes) might work, if the datetime field is sorted. Try also

awk -v SM=$STOPMARK '/QH1/ && $2 >= SM' file
1 Like

Hello Yoda,

Thank you very much for your prompt reply. I tried the following:

awk '$2 >= $STOPMARK && /qh1adm/' logfile

Strangely I am still not getting the output.

Is there a mistake I am making somewhere. Thanks again for your help.

Variable expansion doesn't happen inside single quote. So define an awk variable and use that instead:

awk -v S="$STOPMARK" '$2 >= S && /qh1adm/' logfile
1 Like

Hello RudiC,

Thank you very much for your prompt reply. I tried the following:

awk "/$STOPMARK/,0 {if ($1 ~ /qh1adm/) print}" logfile

$STOPMARK has 20130709213321

Thanks.

---------- Post updated at 06:31 AM ---------- Previous update was at 06:25 AM ----------

Hello RudiC,

 
awk -v SM=$STOPMARK '/qh1adm/ && $2 >= SM' logfile

worked.

Thanks again.

---------- Post updated at 06:41 AM ---------- Previous update was at 06:31 AM ----------

Hello Yoda,

The above command works perfectly! Thank you very much for your help.

---------- Post updated at 07:35 PM ---------- Previous update was at 07:41 AM ----------

Hello RudiC, Hello Yoda,

Now I am getting the desired output:

 
qh1adm 20130711151154 : tp import all QH1 u6 -Dsourcesystems=BFI,EBJ
qh1adm 20130711151155 : tp import all QH1 u6 -Dsourcesystems=E7B,B17
qh1adm 20130711151200 : tp import all QH1 u6 -Dsourcesystems=BFI,EBJ

I was just wondering how to assign this output to an array. The reason is I want to count how many records are returned. Thanks a lot for your help.

You could count and print number of records in awk:

awk -v S="$STOPMARK" '$2 >= S && /qh1adm/ { ++c } END { print c }' logfile
1 Like

NB: /qh1adm/ matches everywhere in the line.
Safer is $1~/qh1adm/ (matches in first field) and even safer is $1=="qh1adm" (is equal to the first field).