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

Clash 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.
grep
add a comment |Â
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.
grep
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
add a comment |Â
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.
grep
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
grep
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
add a comment |Â
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
add a comment |Â
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
+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
add a comment |Â
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
+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
add a comment |Â
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
+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
add a comment |Â
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
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
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
add a comment |Â
+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
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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