• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 293
  • Last Modified:

command line regexp to find a word not coupled with another word

Hello

What would i use (e.g grep or sed or awk) to match lines which contain a particular word (word1) so long as it isn't coupled with another specific word (word2)

For example
word1 = Monday
word2=Sunday

Line1 = Monday Tuesday <===match
LIne2 = Monday Wednesday <===match
Line3 = Monday Sunday <=== no match due to presence of Sunday

I only know the very minimum of sed and awk

thanks
0
andieje
Asked:
andieje
  • 4
  • 3
  • 3
  • +1
1 Solution
 
wesly_chenCommented:
awk ' /word1/ && !/word2/' file
0
 
wesly_chenCommented:
verify

awk '/Monday/ && !/Sunday/' << END
Monday Tuesday
Monday Wednesday
Monday Sunday
END

Output:
Monday Tuesday
Monday Wednesday

0
 
farzanjCommented:
egrep "Sunday|Monday" filename
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
farzanjCommented:
Sorry

Take this one

grep "Monday" filename | grep -v "Sunday"
0
 
arnoldCommented:
farzanj, your condition will match
Sunday Monday
Though it is not clear whether the "coupled" means anywhere on the line.
Or whether the person means word2 follows immediately after word1.
0
 
farzanjCommented:
@arnold
The second one doesn't.  
grep "Monday" filename | grep -v "Sunday"
It would work no matter the words are together or not.   Sorry, didn't get your point.
0
 
arnoldCommented:
The asker in the example gave the condition as Monday Sunday to be rejected,
Your grep | grep -v will also exclude
Sunday Monday entries.
Monday follows Sunday

etc.

I read the request to make sure to exclude entries where word2 follows word1.
word1 word2 (excluded)
0
 
andiejeAuthor Commented:
sorry, i should have been clearer. the order of the words does not matter as i do not know what the order will be. i didn't think of that
0
 
andiejeAuthor Commented:
I didn't realise you could do that with awk - that's good.
0
 
andiejeAuthor Commented:
can i make it case insensitive and cmbine more conditions in brackets
awk '( /word1/ && !/word2/) || /word3)' file

will have a play with this now. it seems there a few basic text functions like awk sort paste cut and join which are really worth knowing
0
 
farzanjCommented:
You can do easily with grep

egrep "word1|word3" file | grep -v "word2"
0
 
arnoldCommented:
Depending on what it is you need to do and how often you need this process to run, a perl script might be an option.

word3's inclusion should not be cancelled by word2's existence on the same line.
But I do not have the full scope of the user's "rules" for data extraction.
0

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

  • 4
  • 3
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now