Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Determine if 2 words exist on any line in a text file 1

Status
Not open for further replies.

eholdo

Technical User
May 30, 2002
31
US
Hello,

I can't believe I don't remember how to do this, but I need to determine if 2 words exist on any line in a text file, with an unknown number of characters before, between or after the 2 words on the line. Such as

Words to look for are cat and dog

The cat looks at the dog and
Cat is not dog

should both return $status == 0

I need to do this in a csh script.

Thanks!
 
nawk '/cat/ && /dog/' file vlad
+---------------------------+
|#include<disclaimer.h> |
+---------------------------+
 
why not simply:

grep 'cat.*dog' filename >/dev/null 2>&1 && exit 0 || exit 1

?? vox clamantis in deserto.
 
'cause:
cat.*dog != dog.*cat vlad
+---------------------------+
|#include<disclaimer.h> |
+---------------------------+
 
vlad i don't really follow you, the question was
cat+dog, nobody speaks about dog+cat..
sure you know the grep family
egrep 'cat.*dog|dog.*cat'
or less appropriate (it match more then needed)...
grep '[cd][ao][tg].*[cd][ao][tg]' vox clamantis in deserto.
 
jamisar,
that's much better. The OP did not mention the particular order of strings to be 'matched'. Therefore, the order should not be important. I hope your read of what needed to be done is the same as mine.

On the related point.... The OP is talking about words and not simply strings. That makes the task a little bit more difficult and the above aproach might not be one that's needed. Are the below examples satisfy the OPs definitions?

catination and world dogmatism
dogmas of catholics
dog's paws and cat's fur
vlad
+---------------------------+
|#include<disclaimer.h> |
+---------------------------+
 
vgersh99, good point. As these are words, I do need to match the whole word, and not just a component of the word.

Also, I've tried using the * wildcard in grep and it does not seem to work under Solaris 2.6. Any thoughts why?

Thanks again for everyone's help.
 
The delimeters \< \> would do it for you but they are only inplemented in gawk, AFAIK. You could try this on your system to see if it works for you. Otherwise, we would need to know all the possible word delimiters in you data.

$0 !~ /\<[Cc][Aa][Tt]\>/ || $0 !~ /\<[Dd][Oo][Gg]\>/ {exit 1} CaKiwi
 
Well then, mine's easy enough to modify...

[tt]cat file | grep &quot;[\b]*cat[\b]*&quot; | grep &quot;[\b]*dog[\b]*&quot;[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top