Check multiple patterns in awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Check multiple patterns in awk
# 1  
Old 10-14-2015
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:
------------
Code:
 1 2 3 4 5 6

Inputs:
Code:
 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 check if $a==$b
Code:
awk '/$a/ && /$b/' output.txt ##worked when executed alone 
  
BEGIN { count=0;} {$0~/$a/&&/$b/} { count++; } END { print "Numbers matching =",count;}' output.txt  ##Not working.always returns 1

Please point out my mistake

Last edited by Don Cragun; 10-14-2015 at 02:23 PM.. Reason: Add CODE and ICODE tags.
# 2  
Old 10-14-2015
Hello kannan13,

Welcome to forums hope you will enjoy learning here. Please use code tags as per forum rules for Inputs/codes/commands which you are using into your post as per forum rules. Now coming to your post, $a will denote value of a variable but in awk it wouldn't work like that way, there is NO need of $ sign to get the value of variables here. An example for this is as follows.
Code:
awk -vvariable=10 'BEGIN{print variable}'

Output will be as follows.
Code:
10

Now for your requirement as far as I understood your requirement, you have 2 variables named a and b and if there value is present in file(Taking your sample file only, because if file has multiple lines then we need to be more specific about your requirement as you need to tell us either you need to check variable in whole file or line by line is fine.) and their own values are equal then print value 0 in output.
Code:
awk -va=1 -vb=1  '{for(i=1;i<=NF;i++){e=a==$i?q++:q;e=b==$i?q++:q;};}{if(e==2 && a==b){print 0}}'  Input_file

Because I have taken an example where values or variable a and b are same it will show output as follows.
Code:
0

If you have any doubts or your requirement is not meeting this solution, I would suggest you to show your sample input with all conditions with their expected output and your O.S details too, hope this helps you.


Thanks,
R. Singh
# 3  
Old 10-14-2015
Thanks for the help.

My OS is Ubuntu.When I tried your code,it did not give me 0.
File will have only 1 line with the values I listed.Input is the same as listed.

Thanks
# 4  
Old 10-14-2015
Please use code tags as required by forum rules!

shell variables (your a and b ) are not available in awk per se. On top, single quoting prevents shell expansion. So it's difficult to believe that your first sample code worked.
In awk,
/.../ is a regex constant; you can't have variables in there
$0~/$a/ and /$b/ are equivalent (but would not work as expected!)
{...} is an action, not a pattern (so {count++} will always be executed)

Try
Code:
awk -vA=$a -vB=$b '
$0 ~ A && $0 ~ B        { count++}
END                     {print "Numbers matching =",count+0
                        }
'  file

You'll still need to make sure there are no false positives (as "100" would match "1")
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk Replace Multiple patterns within a list_file with One in target_file

I'm facing a problem 1) I got a list_file intended to be used for inlace replacement like this Replacement pattern ; Matching patterns EXTRACT ___________________ toto ; tutu | tata | tonton | titi bobo ; bibi | baba | bubu | bebe etc. 14000 lines !!! ... (5 Replies)
Discussion started by: mpvphd
5 Replies

2. Shell Programming and Scripting

Replacing matched patterns in multiple files with awk

Hello all, I have since given up trying to figure this out and used sed instead, but I am trying to understand awk and was wondering how someone might do this in awk. I am trying to match on the first field of a specific file with the first field on multiple files, and append the second field... (2 Replies)
Discussion started by: karlmalowned
2 Replies

3. Shell Programming and Scripting

Match multiple patterns sequentially in order - grep or awk

Hello. grep v2.21 Debian 8 I wish to search for and output these patterns in order; "From " "To: " "Subject: " "Message-Id: " "Date: " "To: " grep works, but not in strict order... $ grep -a -E "^From |^Subject:|^From: |^Message-Id: |^Date: |^To: " InboxResult; From - Wed Feb 18... (10 Replies)
Discussion started by: DSommers
10 Replies

4. 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

5. 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

6. Shell Programming and Scripting

awk extract strings matching multiple patterns

Hi, I wasn't quite sure how to title this one! Here goes: I have some already partially parsed log files, which I now need to extract info from. Because of the way they are originally and the fact they have been partially processed already, I can't make any assumptions on the number of... (8 Replies)
Discussion started by: chrissycc
8 Replies

7. Shell Programming and Scripting

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... (7 Replies)
Discussion started by: kcboy
7 Replies

8. 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

9. Shell Programming and Scripting

Awk to Count Multiple patterns in a huge file

Hi, I have a file that is 430K lines long. It has records like below |site1|MAP |site2|MAP |site1|MODAL |site2|MAP |site2|MODAL |site2|LINK |site1|LINK My task is to count the number of time MAP, MODAL, LINK occurs for a single site and write new records like below to a new file ... (5 Replies)
Discussion started by: reach.sree@gmai
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