Issue
I tried to count the number of words that have at least 2 consecutive letters the same,
I tried something using the grep command, first I let every word show in a new line using the tr command, after that, I used grep to match the pattern,
tr ' ' '\12' | grep -c '\(.*\).\1'
but this gave me the count of all words and when I used the same command without the -c option with grep I saw only letters from those words matched highlighted in red like this:
https://i.stack.imgur.com/Vfjys.png
I also tried using :
tr ' ' '\12' | '\(.*\).\1' | wc -l
and got 24 instead of 5.
Solution
Given this input text lorem-ipsum.txt
:
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum
Same text as above with its double letters highlighted:
Lorem ipsum dolor sit amet consectetur adipiscing elit sed do eiusmod tempor incididunt ut labore et dolore magna aliquaUt enim ad minim veniam quis nostrud exercitation (1) ullamco laboris nisi ut aliquip ex ea (2) commodo consequatDuis aute irure dolor in reprehenderit in voluptate velit (3) esse (4) cillum dolore eu fugiat (5) nulla pariaturExcepteur sint (6) occaecat cupidatat non proident sunt in culpa qui (7) officia deserunt (8) mollit anim id est laborum
You count words containing consecutive double letters with:
tr -c '[:alpha:]' '\n' < lorem-ipsum.txt | grep -c '\(.\)\1'
tr -c '[:alpha:]' '\n'
: Turns non-alpha characters to newline, so each word is placed on its own line in the stream.grep -c '\(.\)\1'
: Counts occurrences of any captured character\(.\)
followed by itself\1
.
Test assertion:
[ 8 -eq "$(tr -c '[:alpha:]' '\n' < lorem-ipsum.txt | grep -c '\(.\)\1')" ]
Answered By - Léa Gris Answer Checked By - Marilyn (WPSolving Volunteer)