awk multiple search and if conditions


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk multiple search and if conditions
# 1  
Old 04-04-2014
awk multiple search and if conditions

Hi

I wanted to search for 2 patterns. These patterns are matched only if the if condition is matched for example:

This is the kind of command that I have in mind which is obviously not correct:
Code:
awk '/abc/ if ($1>10) {print);/xyz/ if ($2>5) {print)' myfile

myfile:
Code:
12 14 3  20 45 abc
21  4 30 21 12 xyz
33 19 10 21 49 xyz
9 23 54 21 26 abc
55 41 13 62 24 abc
12 44 16 54 62 xyz


Basically the output should look like this:

Code:
12 14 3  20 45 abc
33 19 10 21 49 xyz
55 41 13 62 24 abc
12 44 16 54 62 xyz

Thanks

Last edited by Scrutinizer; 04-04-2014 at 01:43 AM.. Reason: Added code tags ....
# 2  
Old 04-04-2014
Hi, are you looking for something like this?

Code:
awk '(/abc/ && $1>10) || (/xyz/ && $2>5)' file

Output:
Code:
12 14 3  20 45 abc
33 19 10 21 49 xyz
55 41 13 62 24 abc
12 44 16 54 62 xyz


Last edited by Scrutinizer; 04-06-2014 at 10:48 PM..
This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 04-04-2014
Thanks Scrutinizer. Yes. It works.

Now I just realize that I want the first pattern to match only if the second pattern matches. For example let say pattern abc and xyz appear in the file alternately:

Code:
12 14 3  20 45 abc
21  4 30 21 12 xyz
9 23 54 21 26 abc
33 19 10 21 49 xyz
55 41 13 62 24 abc
12 44 16 54 62 xyz

With the same requirement as in my first post, now I want abc only matches if xyz matches.

Thanks again
# 4  
Old 04-04-2014
That is a little bit more complicated. Try something like:
Code:
awk 'NR%2{p=$0; m=$1; next} p~/abc/ && m>10 && /xyz/ && $2>5{print p ORS $0}'  file

output:
Code:
55 41 13 62 24 abc
12 44 16 54 62 xyz


Last edited by Scrutinizer; 04-06-2014 at 10:48 PM..
This User Gave Thanks to Scrutinizer For This Post:
# 5  
Old 04-04-2014
Actually your first proposal would work once your add/replace some curly brackets:
Code:
awk '/abc/ {if ($1>10) {print}};/xyz/ {if ($2>5) {print}}' file
12 14 3  20 45 abc
33 19 10 21 49 xyz
55 41 13 62 24 abc
12 44 16 54 62 xyz

This User Gave Thanks to RudiC For This Post:
# 6  
Old 04-07-2014
Thanks RudiC,

How about the second requirement? Is there a simpler way to do this? The first requirement is still the same but I want "abc" matches if only "xyz" matches too.

---------- Post updated at 10:24 PM ---------- Previous update was at 08:44 PM ----------

I have finally solved this. I found some examples on the web, so here it is:

Code:
awk '/abc/ {if ($1>10) {x=$0}};/xyz/ {if ($2>5) {print x;print}}' file


Last edited by Scrutinizer; 04-07-2014 at 02:10 AM..
# 7  
Old 04-07-2014
I think you would need to adjust your solution so that x gets erased once a match for a different line gets found and then you would also need to test if x contains something before printing both x and the current line.

Otherwise you may end up printing an old x from a previous match or print the current line even though there never a was previous match for x..

Of course if there is a possibility that there are lines that match both /abc/ and /xyz/ you would need to make provisions for that (in this case it would print the line, but that may or may not be the intent)

Last edited by Scrutinizer; 04-07-2014 at 02:28 AM..
This User Gave Thanks to Scrutinizer 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

awk to change value of field using multiple conditions

In the below awk in the first step I default Classification NF-1 to VUS. Next, I am trying to change the value of Classification (NF) to whatever CLINSIG (NF-1) is. If there is only one condition everything works great, but if there are two conditions it does not work. Is the syntax used... (4 Replies)
Discussion started by: cmccabe
4 Replies

2. Shell Programming and Scripting

Using awk to parse multiple conditions

There are 4 ways the user can input data and unfortunately the parse rules for each are slightly different. The first condition works great and the input file is attached for the second condition. Conditions 3 and 4 will follow I'm sure I will have trouble with them and need help as well. The... (9 Replies)
Discussion started by: cmccabe
9 Replies

3. Shell Programming and Scripting

Multiple Line awk search and replace

I have a log file that contains many lines but contains the following line three times: related_pin : "t_bypass"; Here are the 3 occurrences and the two lines after from my file.txt: related_pin : "t_bypass"; sdf_cond : "rstq_b"; timing_sense : negative_unate; ... (6 Replies)
Discussion started by: bobbygb2003
6 Replies

4. Shell Programming and Scripting

awk with multiple pattern search

Want to fetch a column with multiple pattern using awk. How to achieve the same. Tried cat test address : 10.63.20.92/24 address : 10.64.22.93/24 address : 10.53.40.91/24 cat test | awk '{print $3}' |awk -F "/" '{print $1}' 10.63.20.92 10.64.22.93 10.53.40.91 Is there any... (2 Replies)
Discussion started by: Manasa Pradeep
2 Replies

5. Shell Programming and Scripting

awk delete/remove rest of line on multiple search pattern

Need to remove rest of line after the equals sign on search pattern from the searchfile. Can anybody help. Couldn't find any similar example in the forum: infile: 64_1535: Delm. = 86 var, aaga 64_1535: Fran. = 57 ex. ccc 64_1639: Feb. = 26 (link). def 64_1817: mar. = 3/4. drz ... (7 Replies)
Discussion started by: sdf
7 Replies

6. Shell Programming and Scripting

Help to search multiple pattern in file with grep/sed/awk

Hello All, I have a file which is having below type of data, Jul 19 2011 | 123456 Jul 19 2011 | 123456 Jul 20 2011 | 123456 Jul 20 2011 | 123456 Here I wanted to grep for date pattern as below, so that it should only grep "Jul 20" OR "Jul ... (9 Replies)
Discussion started by: gr8_usk
9 Replies

7. Shell Programming and Scripting

awk: Multiple search patterns & print in an one liner

I would like to print result of multiple search pattern invoked from an one liner. The code looks like this but won't work gawk -F '{{if ($0 ~ /pattern1/) pat1=$1 && if ($0 ~ /pattern2/) pat2=$2} ; print pat1, pat2}' Can anybody help getting the right code? (10 Replies)
Discussion started by: sdf
10 Replies

8. Shell Programming and Scripting

awk multiple-line search and replace one-liner

Hi I am trying to search and replace a multi line pattern in a php file using awk. The pattern starts with <div id="navbar"> and ends with </div> and spans over an unknown number of lines. I need the command to be a one liner. I use the "record separator" like this : awk -v... (8 Replies)
Discussion started by: louisJ
8 Replies

9. Shell Programming and Scripting

specifying multiple conditions in AWK

how can i specify more than 1 consition in the following AWK statament?? i.e. if $2 is ABCD and $3 is MNOP and $4 is KLPM similarly for OR #!/bin/ksh awk -F '' ' $2 == "ABCD" { print $2, $3;}' file.xml (2 Replies)
Discussion started by: skyineyes
2 Replies

10. Shell Programming and Scripting

Multiple search string in multiple files using awk

Hi, filenames: contains name of list of files to search in. placelist contains the names of places to be searched in all files in "filenames" for i in $(<filenames) do egrep -f placelist $i if ] then echo $i fi done >> outputfile Output i am getting: (0 Replies)
Discussion started by: pinnacle
0 Replies
Login or Register to Ask a Question