ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Dynamite! As a follow-on please help this newbie understand what grep did. I read \(.\).* to mean "any one character followed by zero or more of any characters." Is this right? What does the 1.*\1 do for us? Is the whole string \(.\).*\1.*\1 considered a Regular Expression?
ok: [b].[b] is any character. I put it in parentheses \(.\) so it can be referenced later. The \1 does just that. It references the string matched by the expression in \( \). In other words, \1 means "the same character as the one matched by \( \)". Between the \(.\) and the \1 references there's .* which means that the occurences of the matched character may be separated by zero or more other characters.
Another example of using references may be
Code:
sed 's/\(.*\) \(.*\)/\2 \1/'
which swaps two words (or, more exactly, swaps the last word with the rest of the line, if there are more then two words, because .* is greedy)
ok: [b].[b] is any character. I put it in parentheses \(.\) so it can be referenced later. The \1 does just that. It references the string matched by the expression in \( \). In other words, \1 means "the same character as the one matched by \( \)". Between the \(.\) and the \1 references there's .* which means that the occurrences of the matched character may be separated by zero or more other characters. ...
Thank you for the education. This thread is marked SOLVED!
It was simple enough to extend this grep to find words containing 4, 5, and 6 occurrences of the same letter. This is the "sextuple" version which finds words such as dispossesses and indivisibility.
Code:
grep -e '\(.\).*\1.*\1.*\1.*\1.*\1'
The next question is a matter of cosmetics, not function. Is there a way to indicate "repeats?" Something like this:
You offered this elegant one-liner to find words which contain at least 3 of the same character. I've been experimenting with variations on this theme.
Example 1) Find words which contain at least 3 of the character in column 1, words such as " alabaster" or "abracadabra"
Code:
grep -e '\(^.\).*\1.*\1'
This works.
Next, I made the task more difficult.
Example 2) Find words which contain at least 3 of the character in column 2, such as "aardvark".
Example 2) Find words which contain at least 3 of the character in column 2, such as "aardvark".
Code:
grep -e '.\(.\).*\1.*\1'
This doesn't work.
This does not work because your regex takes the character in column 2 and looks for two other occurences of it after the column 2. In the word "aardvark", one of the 'a's is before the \(.\) which is a case your regex doesn't take into account. A solution to this might be adding an additional regex to include this possibility:
Code:
grep -e '.\(.\).*\1.*\1' -e '\(.\)\1.*\1'
where the first regex is your original one and the second will match strings where the first and second characters are the same and the string contains one more anywhere after position 2.
... A solution to this might be adding an additional regex to include this possibility:
Code:
grep -e '.\(.\).*\1.*\1' -e '\(.\)\1.*\1'
where the first regex is your original one and the second will match strings where the first and second characters are the same and the string contains one more anywhere after position 2.
Alas, no joy. This is my code ...
Code:
# Find words which contain at least 3 of the character in column 2,
# such as "aardvark". Method of LQ member millgates.
cat < $WrdLst \
|grep -e '.\(.\).*\1.*\1' -e '\(.\)\1.*\1' \
> $Work08
...and the output includes aardvark (as it should) but also many words which don't qualify. This is a small part of the output file:
# Find words which contain at least 3 of the character in column 2,
# such as "aardvark". Method of LQ member millgates.
cat < $WrdLst \
|grep -e '.\(.\).*\1.*\1' -e '\(.\)\1.*\1' \
> $Work08
...and the output includes aardvark (as it should) but also many words which don't qualify. This is a small part of the output file:
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.