Using grep to find duplicate characters in a word in bash

View: New views
3 Messages — Rating Filter:   Alert me  

Using grep to find duplicate characters in a word in bash

by LHUHCD :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Is there a regular expression that can find duplicate characters in a word?  

For example, for five-letter words, we would use.....
     grep "^.....$"
.....to find any five character word
 
How can we, at least, find five-letter words with duplicate letters in the word using grep in bash?

ample - no
apple - yes
pills - yes
civic - no (two pairs of duplicates)
alana - no (includes "triplicate")

At the very least, I am looking for a grep that finds words with at least a duplicate letter inside the word.

Thanks!


Re: Using grep to find duplicate characters in a word in bash

by Bob Proulx :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

LHUHCD wrote:
> Is there a regular expression that can find duplicate characters in a word?  

Normally I love these types of problems and would jump right in.  But
this one has two problems.  One is that it was sent to bug-bash which
is a mailing list for discussion about bugs and features in the bash
shell and your question is off-topic since it has nothing to do with
bash.  A better place to have asked your question would be
help-gnu-utils which is a generic discussion list for all of the gnu
utilities which would include grep.

Secondly this sounds very suspiciously like a homework assignment.  :-)

Bob

> For example, for five-letter words, we would use.....
>      grep "^.....$"
> .....to find any five character word
>  
> How can we, at least, find five-letter words with duplicate letters in the
> word using grep in bash?
>
> ample - no
> apple - yes
> pills - yes
> civic - no (two pairs of duplicates)
> alana - no (includes "triplicate")
>
> At the very least, I am looking for a grep that finds words with at least a
> duplicate letter inside the word.
>
> Thanks!
>
>
> --
> View this message in context: http://old.nabble.com/Using-grep-to-find-duplicate-characters-in-a-word-in-bash-tp26202300p26202300.html
> Sent from the Gnu - Bash mailing list archive at Nabble.com.



Re: Using grep to find duplicate characters in a word in bash

by Chet Ramey :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

LHUHCD wrote:
> Is there a regular expression that can find duplicate characters in a word?  

Sure.  Match a character according to the specs you want, then match it
again using backreferencing.  For instance, the following grep pattern
will match pairs of alphanumeric characters:  '\([[:alnum:]]\)\1'

This doesn't really have anything to do with bash.
--
``The lyf so short, the craft so long to lerne.'' - Chaucer
                 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, ITS, CWRU    chet@...    http://cnswww.cns.cwru.edu/~chet/