grep -i "^(.).*\1$" sowpods.txt > output.txt

I'm using a scrabble words list I downloaded named "sowpods.txt" and I am trying to use grep to find all of the 7 letter words in the file that start and end with the same letter .The line I have now is giving me a backreference error so I tried using online guides but they were insanely confusing. Could someone help me do this? Thanks!

I'm also on a mac and am using the default terminal.

  • 2
    Please don't make more work for other people by vandalizing your posts. By posting on the Stack Exchange (SE) network, you've granted a non-revocable right, under the CC BY-SA 3.0 license for SE to distribute that content. By SE policy, any vandalism will be reverted. If you want to know more about deleting a post, please take a look at: How does deleting work? – iBooot Feb 12 '19 at 21:22

If you want to use basic regular expression (without option -E), you need a backslash \ before the opening and closing brackets:

grep -i '^\(.\).\{5\}\1$' file

Note that it's good idea to use single quote in your grep regex in order to avoid shell parameter expansion.


This will work as an extended regular expression. You need grep -E:

grep -E -i "^(.).*\1$"

To match 7-letter words you need to replace * with {5}:

grep -E -i "^(.).{5}\1$"
  • I'm a fan of the -x flag: grep -Eix "(.).{5}\1" – Jim L. Jan 30 '19 at 22:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.