Searching multiple patterns using awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Searching multiple patterns using awk
# 1  
Old 07-12-2013
Searching multiple patterns using awk

Hello,

I have the following input file:

Code:
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:

Code:
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:

Code:
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.
Moderator's Comments:
Mod Comment Please use CODE and ICODE tags to make code, sample input, and sample output clear.

Last edited by Don Cragun; 07-12-2013 at 03:40 PM.. Reason: Add CODE and ICODE tags.
# 2  
Old 07-12-2013
Code:
awk '$2 >= 20130711151154 && /QH1/' file

This User Gave Thanks to Yoda For This Post:
# 3  
Old 07-12-2013
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
Code:
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
Code:
awk -v SM=$STOPMARK '/QH1/ && $2 >= SM' file

This User Gave Thanks to RudiC For This Post:
# 4  
Old 07-12-2013
Hello Yoda,

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

Code:
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.

Last edited by Scott; 07-13-2013 at 01:24 AM.. Reason: Code tags
# 5  
Old 07-12-2013
Variable expansion doesn't happen inside single quote. So define an awk variable and use that instead:
Code:
awk -v S="$STOPMARK" '$2 >= S && /qh1adm/' logfile

This User Gave Thanks to Yoda For This Post:
# 6  
Old 07-12-2013
Quote:
Originally Posted by RudiC
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
Code:
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
Code:
awk -v SM=$STOPMARK '/QH1/ && $2 >= SM' file


Hello RudiC,

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

Code:
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 ----------

Quote:
Originally Posted by RudiC
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
Code:
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
Code:
awk -v SM=$STOPMARK '/QH1/ && $2 >= SM' file


Hello RudiC,

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

worked.

Thanks again.

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

Quote:
Originally Posted by Yoda
Variable expansion doesn't happen inside single quote. So define an awk variable and use that instead:
Code:
awk -v S="$STOPMARK" '$2 >= S && /qh1adm/' logfile


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:

Code:
 
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.
# 7  
Old 07-13-2013
You could count and print number of records in awk:
Code:
awk -v S="$STOPMARK" '$2 >= S && /qh1adm/ { ++c } END { print c }' logfile

This User Gave Thanks to Yoda For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Searching multiple patterns and construct SQL

Hello, I have attached 2 files 1) Original_table_definition.txt => which has definition of 3 tables 2) describe_table_output.txt => which has again 3 tables definition gotten thorugh doing a show table or describe table way. Now difference between 3 tables are that tablea has no... (2 Replies)
Discussion started by: nv186000
2 Replies

2. Shell Programming and Scripting

Check multiple patterns in awk

I need to check if 2 values exists in the file and if they are equal print 0. output.txt: ------------ 1 2 3 4 5 6 Inputs: a=1 b=2 My pattern matching code works but I am trying to set a counter if both the pattern matches which does not work.If the count > 0,then I want to... (3 Replies)
Discussion started by: kannan13
3 Replies

3. Shell Programming and Scripting

Replacing multiple line patterns with awk

Hi forum, Can you please help me understand how to look for and replace the below pattern (containing line breaks) and return a new result? Rules: Must match the 3 line pattern and return a 1 line result. I have found solutions with sed, but it seems that sed installed in my system is... (5 Replies)
Discussion started by: demmel
5 Replies

4. Shell Programming and Scripting

Multiple patterns for awk script

Hi, I'm getting stuck when supplying multiple patterns for the below code: awk -F, ' .. .. if ($0 ~ pattern) { .. .. } .. .. ' pattern='$ROW' input_file for the same code I'm trying to supply multiple patterns as given below: awk -F, ' .. .. if( ($0 ~ pattern) && ($0 ~... (6 Replies)
Discussion started by: penqueen
6 Replies

5. Shell Programming and Scripting

[Solved] HP-UX awk sub multiple patterns

Hi, I am using sub to remove blank spaces and one pattern(=>) from the input string. It works fine when I am using two sub functions for the same. However it is giving error while I am trying to remove both spaces and pattern using one single sub function. Working: $ echo " OK => " |awk... (2 Replies)
Discussion started by: sai_2507
2 Replies

6. Shell Programming and Scripting

searching multiple patterns in perl

Hi, I have code like: Output it is comming as: Rels: WM2 Rels: WG2 Rels: 5 - pre/prods.pl Rels: 6 Rels: 7 Rels: 8 Rels: 10 Rels: Int But i want only "Rels: 5" pattern Just above "- pre/prods.pl". By... (7 Replies)
Discussion started by: Anjan1
7 Replies

7. Shell Programming and Scripting

Awk script searching patterns issue

Hi I am having a file like this FILE1 ##################### C16ROTINV_ REFCLK_RXL RXBCLK32_R REFCLK_TXL CLK8_TXLIN RXBCLK32_R DCLK_TXLIN CLK32D_TXL RXACLK32_R ##################### (3 Replies)
Discussion started by: jaita
3 Replies

8. Shell Programming and Scripting

Searching for multiple patterns in a file

Hi All, I have a file in which i have to search for a pattern from the beginning of the file and if the pattern is found , then i have to perform a reverse search from that line to the beginning of the file to get the first occurrence of another pattern. sample input file hey what are you... (8 Replies)
Discussion started by: Kesavan
8 Replies

9. Shell Programming and Scripting

Searching for multiple patterns in files

I have a situation where I need to search for multiple strings (error messages) such as 'aborted' 'file not found' etc in directory having logs. I have put all the error messages in a text file and using the command. grep -f <textfile> <filetobegrepped> I'm doing this thru a script where I... (5 Replies)
Discussion started by: bornon2303
5 Replies

10. UNIX for Dummies Questions & Answers

AWK: Multiple patterns per line

Hi there, We have been given a bit of coursework using awk on html pages. Without giving too much away and risking the wrath of the plagerism checks, I can say we need to deal with certain html elements. There may be several of these elements on one line. My question is, if there are more... (1 Reply)
Discussion started by: Plavixo
1 Replies
Login or Register to Ask a Question