The moose likes Linux / UNIX and the fly likes a question abou making ceratain commands Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Engineering » Linux / UNIX
Bookmark "a question abou making ceratain commands" Watch "a question abou making ceratain commands" New topic

a question abou making ceratain commands

johny doe
Ranch Hand

Joined: Dec 07, 2007
Posts: 78
how many words in the file /usr/share/dict/american-english have exactly two letters x?

print all the words in the file /usr/share/dict/american-english that have exactly two letters b, but not together. For example: baby is OK, while lobby is not

i was told to use egrep

i am not sure its the right way

anyway i cant get it done
i lloked in the man nothing was tald about those problems
what option of egrep to use

how do i build it?
Jeff Rummings
Ranch Hand

Joined: Dec 20, 2007
Posts: 42
Have you tried to do it? Post what you came up with so far.
johny doe
Ranch Hand

Joined: Dec 07, 2007
Posts: 78
i looked in the manual of this command
i didnt find there any clues
with option to work??
can you give me a hint
[ January 11, 2008: Message edited by: johny doe ]
Jeff Rummings
Ranch Hand

Joined: Dec 20, 2007
Posts: 42
You could for example

1) find all matches with 2 b's
2) exclude results with more than 2 b's
3) exclude results where b's are consecutive

You can use pipes '|' to redirect output from one command to another

e.g. grep <pattern> <file> | grep -v <pattern> | grep -v <pattern>

Look up the -v option in the man page.
[ January 11, 2008: Message edited by: Arman Sharif ]
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11404

Arman's suggestion is a good way to start.

I would recommend that you start with his step 1. Show us what command you used to get that, and a couple of lines of the output.

At some point you will probably need to use regular expressions. The man page for grep should give you some information on that. However I would recommend you get some sort of output from step 1 before delving too deeply into the regular expressions.

Regards, Andrew

The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
johny doe
Ranch Hand

Joined: Dec 07, 2007
Posts: 78
i found a way how to find the word which have a pair of the same letter

but what i am looking for is the opposite thing
word wich the pair of the same letters are separated

Doug Slattery
Ranch Hand

Joined: Sep 15, 2007
Posts: 294
Hi Johny,

Regular expressions will make your challenge far easier to solve once you understand them.

I'll assume you have the looping part of iterating through all the words in the dictionary covered...

grep uses regular expressions (I take it you figured that out already and I don't mean to insult your intelligence), but the man pages on [e]grep as Arman suggests (at least the ones I've seen) don't delve too deeply on regular expressions. There should be a "See Also" section at the bottom of the man page which will point you to the regex definition.

With that being said, here's a description of how I would approach the problem you're facing:

First off, regex isn't a programming language or anything like that that would allow you to count the number of a particular letter (or pattern) in a string. In your case, each word is a string. Think of regex as a notation for matching substring patterns within a string. The man pages discuss how to construct the substrings. A substring is considered a pattern and you can have multiple patterns in an expression. This is just what you need. To find a pattern consisting of an 'x' followed by another 'x' and no more. To back up a microsec from that statement, regular expressions are evaluated from left to right in the main string until the first pattern (substring) is matched. If there are more patterns, the main string continues to be evaluated after the last character from the previous pattern matched.

Armed with this information, here's the pseudo logic for the problem:
Search from the beginning of the string and find an 'x' pattern.
Continue searching after the first 'x' for another 'x' pattern
Continue searching after the second 'x' for no more 'x' patterns.
If the above three steps work, print the word.

I suspect the third step may give you a stumble with the regex notation. Give it a try and post your results.


-- Nothing is impossible if I'mPossible
Jeff Rummings
Ranch Hand

Joined: Dec 20, 2007
Posts: 42
You could also read a few tutorials on the web. To get you started, take a look at these ones:

It is sorta covered in the JavaRanch Style Guide.
subject: a question abou making ceratain commands
Similar Threads
Resolved: Looking for a free plain text word list
Is there an "existence" binary search in the API
"Even I" vs. "I also"
For the wordsmiths out there
A Riddle... Can someone answer?