[SOLVED] AWK: match multiple strings in the file, print 1 when match and 0 when not
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Thanks! But I just want to match one of them. "A" OR "B". I mean, as long as there is string match "A" or "B", I got a print out 1. If there is there is neither "A" nor "B" in the file, print 0.
Thanks! But I just want to match one of them. "A" OR "B". I mean, as long as there is string match "A" or "B", I got a print out 1. If there is there is neither "A" nor "B" in the file, print 0.
A slight modification to the suggested code: put >=1 in place of ==2. Easy.
Thank you for your code. I tired it. When there is no A and no B in my file, the output is "0" which is what I want. But when there is a A or B in my file, the output is
Code:
1
0
which has a redundant "0" at newline. That is not what I want.
I think your code is the easiest to understand. I don't know where it is wrong, may be the "exit"? Do you have any ideas.
Thanks anyway!
A slight modification to the suggested code: put >=1 in place of ==2. Easy.
Thank you! It works. Could you explain a bit what the meaning of print (one+two>=1)?
what I understood is, this code search A and B separately. If it find a string match "A" it send 1 to variable "one", if it find a string match "B" it send 1 to variable "two". So, if there is no match what value would be send to "one" and "two"? Is that 0??
And then print the result for one+two if one+two larger or equal then 1. So, if there are both "A" and "B" found in the file, one+two should equal to 2, why the output is still 1 in this case?
Or, if there is no "A" and no "B" in the file, own+two now is smaller than 1, why AWK is still able to print 0?
My understanding for the meaning of print (one+two>=1) must be wrong, so what is its correct meaning?
Thanks!!!
Could you explain a bit what the meaning of print (one+two>=1)?
what I understood is, this code search A and B separately. If it find a string match "A" it send 1 to variable "one", if it find a string match "B" it send 1 to variable "two". So, if there is no match what value would be send to "one" and "two"? Is that 0??
Yes. In AWK an uninitialized variable has value 0 if treated arithmetically, is a null string otherwise. The operation one+two causes the variables to be numeric.
Quote:
Originally Posted by cristalp
And then print the result for one+two if one+two larger or equal then 1. So, if there are both "A" and "B" found in the file, one+two should equal to 2, why the output is still 1 in this case? Or, if there is no "A" and no "B" in the file, own+two now is smaller than 1, why AWK is still able to print 0?
The expression is a logical statement which in awk translates to 0 (false) or 1 (true). This is common to many other scripting languages: if you print out a logical variable or expression you will see 0 or 1.
I don't know where it is wrong, may be the "exit"? Do you have any ideas. Thanks anyway!
On behalf of grail, I can tell that indeed the exit statement is the problem. The logic is correct (as soon as it encounters A or B the program should print out 1 and exit, without further processing). However in awk the exit statement doesn't prevent the execution of the END block (if present). A slightly different approach, similar to mine could be:
Code:
awk '/A|B/{var=1}END{print var+0}' file
The notation var+0 is necessary to print out the value of 0. Try to remove the +0 part and see the difference.
Thanks for colucix's excellent explanations and the help from grail and Linux_Kidd, all together I get a deeper understanding for AWK at the end. Thanks a lot for your patient and kind teaching.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.