LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 11-14-2018, 05:48 AM   #1
cvbngrocks
LQ Newbie
 
Registered: Nov 2018
Posts: 10

Rep: Reputation: Disabled
Is my version of awk working?


Salutations everyone

I am trying to get awk to return lines with 3 exact matches of string.

Quote:
i=fox
j=fox
k=fox

awk -v str1="$i" -v str2="$j" -v str3="$k" '$0~str1 && $0~str2 && $0~str3' file

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
 
Old 11-14-2018, 06:04 AM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,126

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
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.
 
Old 11-14-2018, 08:02 AM   #3
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
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.
 
1 members found this post helpful.
Old 11-14-2018, 08:51 PM   #4
cvbngrocks
LQ Newbie
 
Registered: Nov 2018
Posts: 10

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by syg00 View Post
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 View Post
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


Thanks berndbausch for the reply
 
Old 11-15-2018, 12:38 PM   #5
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,791

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
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.
 
Old 11-15-2018, 03:39 PM   #6
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
Quote:
Originally Posted by cvbngrocks View Post
I thought that using the explicit && would mean all three conditions must be true. It did work for the first line
It does mean that all three conditions must be true. But all three conditions are identical in your case. A && A is the same as A or A && A && A.
 
Old 11-15-2018, 05:34 PM   #7
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,126

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
Quote:
Originally Posted by cvbngrocks View Post
I got the syntax right as I found examples online.
I said check you logic, not your syntax.
As explained a couple of times by berndbausch.

Change one of the vars to "fx" and see what happens.
 
Old 11-15-2018, 10:08 PM   #8
cvbngrocks
LQ Newbie
 
Registered: Nov 2018
Posts: 10

Original Poster
Rep: Reputation: Disabled
Code:
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.

I got it now
 
Old 11-16-2018, 01:49 AM   #9
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,791

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
The correct input matters.
 
Old 11-16-2018, 02:23 AM   #10
cvbngrocks
LQ Newbie
 
Registered: Nov 2018
Posts: 10

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by MadeInGermany View Post
The correct input matters.
Thanks

BTW, the next line in the file was:

The fox said, "OK! As long as you don't called it fake."
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] sed inside awk or awk inside awk maddyfreaks Linux - Newbie 4 06-29-2016 01:10 PM
[SOLVED] Once again... awk.. awk... awk shivaa Linux - Newbie 13 12-31-2012 04:56 AM
awk question on handling *.CSV "text fields" in awk jschiwal Programming 8 05-27-2010 06:23 AM
Regarding distribution + kernel version + gcc version + glib version. JCipriani Linux - General 8 04-19-2008 02:54 PM
Some comments on awk and awk scripts makyo Programming 4 03-02-2008 05:39 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:08 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration