How do I grep for words with an exact number of occurrences of a letter?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP








up vote
0
down vote

favorite












I'm trying to write a script that searches a dictionary for words that contain exactly two occurrences of each letter. I just don't the syntax to grep for exactly two occurrences of a letter.










share|improve this question

















  • 2




    Please give us an example.
    – pa4080
    Apr 10 at 14:58










  • Would this do: grep '([[:alpha:]]2)[^[:space:]]*1' Thing is... you can't grep for the words only. You will get the whole line. If you only want the word how about using perl? perl -Mopen=locale -lne 'print $& while /S*([[:alpha:]]2)S*1S*/g'
    – Rinzwind
    Apr 10 at 15:01











  • That would probably work because this dictionary has 1 word per line. Another question: if i was doing a loop with each letter being $i, where would i input the $i? I’m a beginner if you can’t tell @Rinzwind
    – plshelp
    Apr 10 at 17:42














up vote
0
down vote

favorite












I'm trying to write a script that searches a dictionary for words that contain exactly two occurrences of each letter. I just don't the syntax to grep for exactly two occurrences of a letter.










share|improve this question

















  • 2




    Please give us an example.
    – pa4080
    Apr 10 at 14:58










  • Would this do: grep '([[:alpha:]]2)[^[:space:]]*1' Thing is... you can't grep for the words only. You will get the whole line. If you only want the word how about using perl? perl -Mopen=locale -lne 'print $& while /S*([[:alpha:]]2)S*1S*/g'
    – Rinzwind
    Apr 10 at 15:01











  • That would probably work because this dictionary has 1 word per line. Another question: if i was doing a loop with each letter being $i, where would i input the $i? I’m a beginner if you can’t tell @Rinzwind
    – plshelp
    Apr 10 at 17:42












up vote
0
down vote

favorite









up vote
0
down vote

favorite











I'm trying to write a script that searches a dictionary for words that contain exactly two occurrences of each letter. I just don't the syntax to grep for exactly two occurrences of a letter.










share|improve this question













I'm trying to write a script that searches a dictionary for words that contain exactly two occurrences of each letter. I just don't the syntax to grep for exactly two occurrences of a letter.







grep






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Apr 10 at 14:55









plshelp

1




1







  • 2




    Please give us an example.
    – pa4080
    Apr 10 at 14:58










  • Would this do: grep '([[:alpha:]]2)[^[:space:]]*1' Thing is... you can't grep for the words only. You will get the whole line. If you only want the word how about using perl? perl -Mopen=locale -lne 'print $& while /S*([[:alpha:]]2)S*1S*/g'
    – Rinzwind
    Apr 10 at 15:01











  • That would probably work because this dictionary has 1 word per line. Another question: if i was doing a loop with each letter being $i, where would i input the $i? I’m a beginner if you can’t tell @Rinzwind
    – plshelp
    Apr 10 at 17:42












  • 2




    Please give us an example.
    – pa4080
    Apr 10 at 14:58










  • Would this do: grep '([[:alpha:]]2)[^[:space:]]*1' Thing is... you can't grep for the words only. You will get the whole line. If you only want the word how about using perl? perl -Mopen=locale -lne 'print $& while /S*([[:alpha:]]2)S*1S*/g'
    – Rinzwind
    Apr 10 at 15:01











  • That would probably work because this dictionary has 1 word per line. Another question: if i was doing a loop with each letter being $i, where would i input the $i? I’m a beginner if you can’t tell @Rinzwind
    – plshelp
    Apr 10 at 17:42







2




2




Please give us an example.
– pa4080
Apr 10 at 14:58




Please give us an example.
– pa4080
Apr 10 at 14:58












Would this do: grep '([[:alpha:]]2)[^[:space:]]*1' Thing is... you can't grep for the words only. You will get the whole line. If you only want the word how about using perl? perl -Mopen=locale -lne 'print $& while /S*([[:alpha:]]2)S*1S*/g'
– Rinzwind
Apr 10 at 15:01





Would this do: grep '([[:alpha:]]2)[^[:space:]]*1' Thing is... you can't grep for the words only. You will get the whole line. If you only want the word how about using perl? perl -Mopen=locale -lne 'print $& while /S*([[:alpha:]]2)S*1S*/g'
– Rinzwind
Apr 10 at 15:01













That would probably work because this dictionary has 1 word per line. Another question: if i was doing a loop with each letter being $i, where would i input the $i? I’m a beginner if you can’t tell @Rinzwind
– plshelp
Apr 10 at 17:42




That would probably work because this dictionary has 1 word per line. Another question: if i was doing a loop with each letter being $i, where would i input the $i? I’m a beginner if you can’t tell @Rinzwind
– plshelp
Apr 10 at 17:42










1 Answer
1






active

oldest

votes

















up vote
3
down vote













If you want to find exactly two instances anywhere in a word, then you will need to allow for arbitrary non-matching strings before, between, and after the matching characters.



e.g. to find whole words that consist of exactly 2 instances of letter q, case-insensitively, in /usr/share/dict/words:



$ grep -wi '[^q]*q[^q]*q[^q]*' /usr/share/dict/words
Albuquerque
Albuquerque's
Qiqihar
Qiqihar's


If you're not limited to grep specifically, you might want to consider using perl where you can make use of the fact that, when evaluated in a scalar context, the tr command returns the number of transliterations e.g.



perl -ne 'print if tr/qQ/qQ/ == 2' /usr/share/dict/words


In a similar vein, with GNU awk, you could define the character of interest as the field pattern and test the number of fields:



gawk -vFPAT='[qQ]' 'NF==2' /usr/share/dict/words





share|improve this answer






















  • +1, In my ears the question sounds like the answer should be: for i in a..z; do grep -wi "[^$i]*$i[^$i]*$i[^$i]*" /usr/share/dict/words; done :)
    – pa4080
    Apr 10 at 18:28











  • @pa4080 sure - but the OP should be expected to do some work - they said they did say "I just don't the syntax to grep for exactly two occurrences of a letter" after all...
    – steeldriver
    Apr 10 at 18:30










Your Answer







StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "89"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1023684%2fhow-do-i-grep-for-words-with-an-exact-number-of-occurrences-of-a-letter%23new-answer', 'question_page');

);

Post as a guest






























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
3
down vote













If you want to find exactly two instances anywhere in a word, then you will need to allow for arbitrary non-matching strings before, between, and after the matching characters.



e.g. to find whole words that consist of exactly 2 instances of letter q, case-insensitively, in /usr/share/dict/words:



$ grep -wi '[^q]*q[^q]*q[^q]*' /usr/share/dict/words
Albuquerque
Albuquerque's
Qiqihar
Qiqihar's


If you're not limited to grep specifically, you might want to consider using perl where you can make use of the fact that, when evaluated in a scalar context, the tr command returns the number of transliterations e.g.



perl -ne 'print if tr/qQ/qQ/ == 2' /usr/share/dict/words


In a similar vein, with GNU awk, you could define the character of interest as the field pattern and test the number of fields:



gawk -vFPAT='[qQ]' 'NF==2' /usr/share/dict/words





share|improve this answer






















  • +1, In my ears the question sounds like the answer should be: for i in a..z; do grep -wi "[^$i]*$i[^$i]*$i[^$i]*" /usr/share/dict/words; done :)
    – pa4080
    Apr 10 at 18:28











  • @pa4080 sure - but the OP should be expected to do some work - they said they did say "I just don't the syntax to grep for exactly two occurrences of a letter" after all...
    – steeldriver
    Apr 10 at 18:30














up vote
3
down vote













If you want to find exactly two instances anywhere in a word, then you will need to allow for arbitrary non-matching strings before, between, and after the matching characters.



e.g. to find whole words that consist of exactly 2 instances of letter q, case-insensitively, in /usr/share/dict/words:



$ grep -wi '[^q]*q[^q]*q[^q]*' /usr/share/dict/words
Albuquerque
Albuquerque's
Qiqihar
Qiqihar's


If you're not limited to grep specifically, you might want to consider using perl where you can make use of the fact that, when evaluated in a scalar context, the tr command returns the number of transliterations e.g.



perl -ne 'print if tr/qQ/qQ/ == 2' /usr/share/dict/words


In a similar vein, with GNU awk, you could define the character of interest as the field pattern and test the number of fields:



gawk -vFPAT='[qQ]' 'NF==2' /usr/share/dict/words





share|improve this answer






















  • +1, In my ears the question sounds like the answer should be: for i in a..z; do grep -wi "[^$i]*$i[^$i]*$i[^$i]*" /usr/share/dict/words; done :)
    – pa4080
    Apr 10 at 18:28











  • @pa4080 sure - but the OP should be expected to do some work - they said they did say "I just don't the syntax to grep for exactly two occurrences of a letter" after all...
    – steeldriver
    Apr 10 at 18:30












up vote
3
down vote










up vote
3
down vote









If you want to find exactly two instances anywhere in a word, then you will need to allow for arbitrary non-matching strings before, between, and after the matching characters.



e.g. to find whole words that consist of exactly 2 instances of letter q, case-insensitively, in /usr/share/dict/words:



$ grep -wi '[^q]*q[^q]*q[^q]*' /usr/share/dict/words
Albuquerque
Albuquerque's
Qiqihar
Qiqihar's


If you're not limited to grep specifically, you might want to consider using perl where you can make use of the fact that, when evaluated in a scalar context, the tr command returns the number of transliterations e.g.



perl -ne 'print if tr/qQ/qQ/ == 2' /usr/share/dict/words


In a similar vein, with GNU awk, you could define the character of interest as the field pattern and test the number of fields:



gawk -vFPAT='[qQ]' 'NF==2' /usr/share/dict/words





share|improve this answer














If you want to find exactly two instances anywhere in a word, then you will need to allow for arbitrary non-matching strings before, between, and after the matching characters.



e.g. to find whole words that consist of exactly 2 instances of letter q, case-insensitively, in /usr/share/dict/words:



$ grep -wi '[^q]*q[^q]*q[^q]*' /usr/share/dict/words
Albuquerque
Albuquerque's
Qiqihar
Qiqihar's


If you're not limited to grep specifically, you might want to consider using perl where you can make use of the fact that, when evaluated in a scalar context, the tr command returns the number of transliterations e.g.



perl -ne 'print if tr/qQ/qQ/ == 2' /usr/share/dict/words


In a similar vein, with GNU awk, you could define the character of interest as the field pattern and test the number of fields:



gawk -vFPAT='[qQ]' 'NF==2' /usr/share/dict/words






share|improve this answer














share|improve this answer



share|improve this answer








edited Apr 10 at 18:29

























answered Apr 10 at 15:09









steeldriver

63k1198166




63k1198166











  • +1, In my ears the question sounds like the answer should be: for i in a..z; do grep -wi "[^$i]*$i[^$i]*$i[^$i]*" /usr/share/dict/words; done :)
    – pa4080
    Apr 10 at 18:28











  • @pa4080 sure - but the OP should be expected to do some work - they said they did say "I just don't the syntax to grep for exactly two occurrences of a letter" after all...
    – steeldriver
    Apr 10 at 18:30
















  • +1, In my ears the question sounds like the answer should be: for i in a..z; do grep -wi "[^$i]*$i[^$i]*$i[^$i]*" /usr/share/dict/words; done :)
    – pa4080
    Apr 10 at 18:28











  • @pa4080 sure - but the OP should be expected to do some work - they said they did say "I just don't the syntax to grep for exactly two occurrences of a letter" after all...
    – steeldriver
    Apr 10 at 18:30















+1, In my ears the question sounds like the answer should be: for i in a..z; do grep -wi "[^$i]*$i[^$i]*$i[^$i]*" /usr/share/dict/words; done :)
– pa4080
Apr 10 at 18:28





+1, In my ears the question sounds like the answer should be: for i in a..z; do grep -wi "[^$i]*$i[^$i]*$i[^$i]*" /usr/share/dict/words; done :)
– pa4080
Apr 10 at 18:28













@pa4080 sure - but the OP should be expected to do some work - they said they did say "I just don't the syntax to grep for exactly two occurrences of a letter" after all...
– steeldriver
Apr 10 at 18:30




@pa4080 sure - but the OP should be expected to do some work - they said they did say "I just don't the syntax to grep for exactly two occurrences of a letter" after all...
– steeldriver
Apr 10 at 18:30

















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1023684%2fhow-do-i-grep-for-words-with-an-exact-number-of-occurrences-of-a-letter%23new-answer', 'question_page');

);

Post as a guest













































































Popular posts from this blog

Trouble downloading packages list due to a “Hash sum mismatch” error

How do so many people here on Academia.SE, and in general, afford lavish higher education programs?

How do I move numbers in filenames, in a batch renaming operation?