Check for more than one expression using AWK


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Check for more than one expression using AWK
# 1  
Old 02-19-2009
Check for more than one expression using AWK

I have a txt file like below:

testin.txt

AB
BC
CD
DE

I have the following awk script

BEGIN {flag1="N"}
/(AB)|(BC)|(CD)|(DE)/ {flag1="Y"}
END {print flag1}

>awk -f testin.awk testin.txt

Returns
Y

I need to set the flag as Y if all the expressionz, i.e. AB, BC, CD and DE exists. I tried giving & and &&. But it didn't work out.

I am very new to awk. Can someone please help?
# 2  
Old 02-19-2009
Bug

Hi,

below one might help you!!!!

awk '{if(/(AB)|(BC)|(CD)|(DE)/)then;print"Y"}' testin.txt

Thanks
Sha
# 3  
Old 02-19-2009
Quote:
Originally Posted by Shahul
Hi,

below one might help you!!!!

awk '{if(/(AB)|(BC)|(CD)|(DE)/)then;print"Y"}' testin.txt

Thanks
Sha
Hi..

isn't this an OR condition? What I need is that if all the expressions, AB, BC, CD and DE should be present in the file, then it should set flag as 'Y'
# 4  
Old 02-19-2009
..
OR condition:

awk '{if(/(AB)|(BC)|(CD)|(DE)/)
{print "Y"}
else
{print "N"}}' input.lst

Thanks
Sha
# 5  
Old 02-19-2009
Quote:
Originally Posted by Shahul
..
OR condition:

awk '{if(/(AB)|(BC)|(CD)|(DE)/)
{print "Y"}
else
{print "N"}}' input.lst

Thanks
Sha
sorry...i don't get this....

what I think this will do is that if(either of the expressions are present), then set to Y; else set to N.

What I need is if (all expressions are present), then set to Y;else set to N.
# 6  
Old 02-19-2009
Try this:
Code:
$ cat buf1
AB
BC
CD
DE
$ echo -e 'AB\nBC\nCD\n' | cmp -s - buf1 && echo 'Y'
$ echo -e 'AB\nBC\nCD\nDE' | cmp -s - buf1 && echo 'Y'
Y

# 7  
Old 02-19-2009
Bug

Thanks for the replies...I got over it by declaring a counter. Since I have to do it for a big file, the way with the counter is working pretty well...thanks for the replies..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

How to evaluate expression under awk?

I have to display only those subscribers which are in "unconnected state" and the date is 90 days older than today's date. Below command is used for this purpose: cat vfsubscriber_20170817.csv | sed -e 's/^"//' -e '1d' | \ nawk -F '",' '{if ( (substr($11,2,4) == 2017) && ( substr($11,2,8)... (1 Reply)
Discussion started by: dia
1 Replies

2. Shell Programming and Scripting

Evaluate Expression within awk

I want to create a conditional expression string and pass in an awk script. My script is as below... comm="\$3 == "hello"" awk -F "^T" -v command="${comm}" ' { if ( command ) { print "hye" } }' testBut the statement "if ( command )" always evaluates to true which is not... (5 Replies)
Discussion started by: Saikat123
5 Replies

3. Shell Programming and Scripting

awk print expression

Hi All, I have a doubt in awk print exp. Where in some awk commands have seen a digit 1 appended at the end of the awk ,didnt remember the command . like .. cat file |awk '{print }1' Could some one help in understanding these cases where we use them. Regards, Ganesh, (2 Replies)
Discussion started by: rmkganesh
2 Replies

4. Shell Programming and Scripting

Help in understanding awk expression

Hi, Could somebody help me in understanding the following awk expression: awk -v n="POINT" '/%/{print $0 "\n" n ;next}1' < file name Thanks, Arun (6 Replies)
Discussion started by: arun_maffy
6 Replies

5. UNIX for Dummies Questions & Answers

What is the meaning of this awk expression using sub?

Cannot understand what the "&" in the sub expression is supposed to do. {sub(/^/,"&" s)} (3 Replies)
Discussion started by: kristinu
3 Replies

6. Shell Programming and Scripting

awk reg expression

Hello, I have thousand of messages (HL7), I want to use awk to extract only the ones that have a particular value in pv1.18 Each record in the file is the whole HL7 message, ie. when I print $0 I get the whole message MSH EVN PID etc. ,there is an x0d between the segments. I would like to use a... (3 Replies)
Discussion started by: gio001
3 Replies

7. Programming

Perl regular expression to check string ending

Hi, I am trying to write a regular expression in perl to check if the string end's with "numbers-numbers" or "-numbers". I experimented something like m/\d*-\d*$/ , but this is not solving my problem. Can anyone help me in writing this expression? Well spelled titles and proper use of code... (2 Replies)
Discussion started by: successlin
2 Replies

8. UNIX for Dummies Questions & Answers

Awk inside Awk expression

Hi, It can be used awk inside other Awk?. I need to get another text processing while other text process. Thank you. (2 Replies)
Discussion started by: pepeli30
2 Replies

9. UNIX for Dummies Questions & Answers

regular expression and awk

I can print a line with an expression using this: awk '/regex/' I can print the line immediately before an expression using this: awk '/regex/{print x};{x=$0}' How do I print the line immediately before and then the line with the expression? (2 Replies)
Discussion started by: nickg
2 Replies

10. Shell Programming and Scripting

filename extension check - regular expression

How to compare the file name for "zip" or "ZIP" extension. I can put one more || condition to check the upper case in the below: if ]; then Is there any better way to compare using regular expressions. Thx in advance. (4 Replies)
Discussion started by: devs
4 Replies
Login or Register to Ask a Question