2

I'm trying to find the set of words (if any) that contain two consecutive e’s, and also contains two y’s. But so far I tried following commands ,

grep -e '[y&y].*[ee]' -e '[y&y].*[ee]' words2.txt 
grep -e 'y.y' -e 'ee' words2.txt
grep -X '.*[y.y].*&.*[ee].*' words2.txt 
grep -X '.[*y.y].*&.[*ee].*' words2.txt

some of these commands gives either two y's or two consecutive e's, but don't give a word that contains both of these conditions. What should I do now?

4
  • Does your grep support PCRE?
    – heemayl
    Apr 26, 2017 at 4:46
  • what is -X option? or is it type for -x? also, you need to add some sample input and output, it will show whether there are only single words in a line or multiple among other uses...
    – Sundeep
    Apr 26, 2017 at 5:06
  • 1
    @Sundeep, ast-open's grep has a -X option for augmented regexps that does support a & conjunction operator. Apr 28, 2017 at 16:08
  • @StéphaneChazelas thanks for the info... haven't come across augmented regexps before... will look it up :)
    – Sundeep
    Apr 28, 2017 at 16:13

3 Answers 3

2

I'm assuming your file looks something like this:

beeyy yeey open iee eey yeefy
funny reenydy jyyee

and is called words2.txt. Yes, the contents are kind of random. This will display the set of words containing consecutive ee and two y characters:

< words2.txt tr ' ' '\n' | grep 'ee' | grep 'y.*y'

This first ensures that each word is on its own line with tr, then greps for two consecutive e, then greps again for two (possibly non-consecutive) y. Since each grep is a filter, the end result is the set of words containing both consecutive ee and two y.

On my input, the result is:

beeyy
yeey
yeefy
reenydy
jyyee
3
  • thanks so much, I tried so hard yesterday :) It worked at last
    – Batman
    Apr 26, 2017 at 5:00
  • Note that it also find ybeeyey (3 ys). Apr 27, 2017 at 11:22
  • @StéphaneChazelas Indeed. I interpreted the question to allow for that. It will also catch words with three consecutive e
    – Fox
    Apr 27, 2017 at 12:41
2

Assuming you want exactly two ys and not at least two ys, with GNU grep if built with PCRE support:

grep -Pow '(?=\w*y\w*y)(?!\w*y\w*y\w*y)\w*ee\w*'

Or with ast-open grep (2014-01-26 or newer):

grep -Xow '\w*ee*\w*&\w*y\w*y\w*&(\w*y\w*y\w*y\w*)!'

If you also want at least one sequence of exactly two es (and not 3 or more), that is for instance match on eeyy or eeyyeee but not eeeyy), with GNU grep -P:

grep -Pow '(?=\w*y\w*y)(?!\w*y\w*y\w*y)\w*(?<!e)ee(?!e)\w*'
1

Just a variant of Fox's answer:

grep -Po '\w*ee\w*'  words.txt | grep 'y.*y'

if you want exactly 2 ys:

... | grep -v 'y.*y.*y' 

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .