grep1-3 Exercise 2: Repeated Words and Doubled Letters ======================================================= cat > text.txt <<'EOF' This is is a test of the the system Nothing repeated here Bookkeeper keeps books It was the best of times, it was the worst of times a a a EOF echo "Repeated words (any case):" grep -oiE '\b([a-z]+) \1\b' text.txt echo echo "Words with a doubled letter:" grep -oiE '\b[a-z]*([a-z])\1[a-z]*\b' text.txt Output: Repeated words (any case): is is the the a a Words with a doubled letter: Bookkeeper keeps books WHY THIS WORKS AS AN ANSWER --------------------------- ([a-z]+) captures a word and \1 demands the same text again, with -i making the comparison case-insensitive. \b at both ends stops a match from starting or ending inside a word (without the closing \b, 'the then' would wrongly report 'the the': I tried it). The second command finds a word with a doubled letter: [a-z]* any letters, then ([a-z]) one letter captured, then \1 the same letter, then more letters. \b at both ends makes -o print the whole word rather than just the middle. 'a a a' gives one match, 'a a': matching consumes text, so the third 'a' has nothing left to repeat. Back-references with -o are the way to see exactly what was found.