0

I'm using a scrabble words list I downloaded named "sowpods.txt" and I am trying to use grep to find all of words with these criteria:

  • 7 letter word
  • Starts and ends with the same letter
  • Has the same second ,fourth, and sixth letter
  • Has a different 3rd and 5th letter

The line I have so far

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

is giving me a backreference error so I tried using online guides but they were insanely confusing. Is this even possible? If so could someone please help?

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

  • 1
    Your previous question showed you need grep -E or to escape parentheses. This should get rid of the backreference error, you will be able to continue now. Why are you asking about (partially) the same issue? Don't the solutions to the other question work on Mac? If they don't, the right thing to do is to give feedback to users who tried to help you there and solve the other question first, instead of raising the same issue for the second time. – Kamil Maciorowski Oct 15 '18 at 5:10
  • Ah, I did not understand that the E was the problem, I just put in the line that they had given me. Although I actually do not know where to go from the line of code I have now :(. – Kappa123 Oct 15 '18 at 5:23
  • 1
    OK then. If the other question is solved then please accept one of the answers there (take our tour to see how it works). You should also edit the current question so it doesn't mention the "backreference error", because you know how to cope with it and it's no longer the issue (nor a part of the issue); concentrate on whatever the real issue is now. If the "backreference error" was your only issue then this question should be deleted or at least closed as a duplicate of the other one. – Kamil Maciorowski Oct 15 '18 at 5:57
  • Can the second letter be the same as the first? Can the third be the same as the second? ... Please, edit your question and add test cases. – Toto Oct 15 '18 at 8:48
1

Using -P option (PCRE) if available on your system:

grep -P '^(?=[a-zA-Z]{7}$)(.)(?!\1)(.)(?!\1)(?!\2)(.)\2(?!\1)(?!\2)(?!\3).\2\1$' inputfile

Explanation:

^
  (?=[a-zA-Z]{7}$)  : positive lookahead, zero-length assertion that make sure we have exactly 7 letters. You may use \pL{7} if you want to deal with any laguage
  (.)               : first letter, captured in group 1
  (?!\1)            : negative lookahead, zero-length assertion that make sure we don't have the same letter as in group 1 after
  (.)               : second letter, captured in group 2
  (?!\1)            : negative lookahead, zero-length assertion that make sure we don't have the same letter as in group 1 after
  (?!\2)            : negative lookahead, zero-length assertion that make sure we don't have the same letter as in group 2 after
  (.)               : third letter, captured in group 3
  \2                : fourth letter == second letter
  (?!\1)            : negative lookahead, zero-length assertion that make sure we don't have the same letter as in group 1 after
  (?!\2)            : negative lookahead, zero-length assertion that make sure we don't have the same letter as in group 2 after
  (?!\3)            : negative lookahead, zero-length assertion that make sure we don't have the same letter as in group 3 after
  .                 : fifth letter
  \2                : sixth letter == second letter
  \1                : seventh letter == first letter
$

DEMO

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.