Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
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.
the fox kill the other fox and the fox felt bad
the fox is dead
Why is awk returning the second line with one fox string? Is my syntax wrong or is something missing? How can I make awk return the first line only? Thanks
Your version of awk is working fine. In almost all occasions when you think awk (or any other language/interpreter) is failing, think again - it will be a user error.
As in this case.
Read your logic more carefully - then try with different variable assignments and see what happens. Learning is trying things.
i hope svg00 forgives me, but here is a clue: You check three times whether the line contains the string fox. The result is the same as checking once, twice or 100 times.
Your version of awk is working fine. In almost all occasions when you think awk (or any other language/interpreter) is failing, think again - it will be a user error.
As in this case.
Read your logic more carefully - then try with different variable assignments and see what happens. Learning is trying things.
I got the syntax right as I found examples online.
Thanks syg00 for the replay
Quote:
Originally Posted by berndbausch
i hope svg00 forgives me, but here is a clue: You check three times whether the line contains the string fox. The result is the same as checking once, twice or 100 times.
I didn't know hat.
I thought that using the explicit && would mean all three conditions must be true. It did work for the first line . LOL
The && is correct, but you need 3 different checks:
if {a word matches str1} && {a different word matches str2} && {yet a different word matches str3}
It can be implemented by 3 nested loops, where the inner loop searches right from the current word in the outer loop...
Or by a regular expression. That is a bit tricky; I found the following solution (for Posix-compatible awk)
Code:
awk -v str1="$i" -v str2="$j" -v str3="$k" 'BEGIN { s="[[:space:]]"; gap=(s "(.*" s ")?") } (" " $0 " ") ~ (s str1 gap str2 gap str3 s)' file
In the long RE string the str3 is right from the str2 that is right from the str1.
Last edited by MadeInGermany; 11-15-2018 at 01:16 PM.
i=trump; j=fox; k=toupee;awk -v str1="$i" -v str2="$j" -v str3="$k" '$0~str1 && $0~str2 && $0~str3' file
trump asked the fox if he can use the fox's fur as a toupee
You guys are right. I used three different items and awk return the line with those three matches.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.