Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

This is a pretty basic question, but I can't get my head around it. I'm learning Linux and one of the questions i've been given is;

*The word sentimentalment includes the same three characters (e.g. "ent") which appear three times. The word "blayblapblam" also contains the same three characters repeated three time (e.g. "bla").

How many words can you find which contain any three characters repeated three times, like the examples "sentimentalment" and "blayblapblam", but which also begin with lower case "d". Use /usr/share/dict/words as your list of possible words and grep to find the answer. The "d" is not one of the characters considered when detecting the three-character strings.*

So far, I can return instances where the same three letters appear twice;

grep -E '^d(...).*\1' /usr/share/dict/words > output

Which to me reads, look for a word beginning with 'd', then combination of three letters, 0 or more characters before the same group (1) appearing again.

I've tried the following;

grep -E '^d(...).*\1.*\1' /usr/share/dict/words > output

Which if my understanding is correct (which it obviously isn't), returns group one, then zero or more characters, then group one again.

Can someone point out where I'm going wrong? Any help is appreciated.

share|improve this question
    
Just to avoid any confusion, neither "sentimentalment" nor "blayblapblam" are words or even common proper nouns in the English language, which is what I assume /usr/share/dict/words contains. I was unable to find any words that match the pattern described above in my local words file. –  jw013 Nov 1 '12 at 14:36
add comment

1 Answer 1

It seems you fixed the three letters to go right after the d. Perhaps you would need something like this instead:

grep -E '^d.*(...).*\1' /usr/share/dict/words > output

which would make your 3-pattern search into

grep -E '^d.*(...).*\1.*\1' /usr/share/dict/words > output

share|improve this answer
    
I believe this answer is exactly what you need. Bear in mind that on my system it returns no results from /usr/share/dict/words. It is, nevertheless, correct. –  terdon Nov 1 '12 at 14:28
    
@terdon I don't have /usr/share/dict/words but tried it on some sample input, and it seems to have behaved as expected. –  gt6989b Nov 1 '12 at 14:42
    
yes it does, absolutely, that's why I upvoted it :). I am just warning the OP that if there is no output, it does not mean that your solution is wrong. –  terdon Nov 1 '12 at 15:16
    
@terdon Thank you... wonder why it takes forever to get such one-liners accepted... –  gt6989b Nov 1 '12 at 15:38
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.