1

I have a bash script where I'm trying to recognize palindromes from a given words.txt file, I think I'm on the right track using grep but I'm not sure. I have to use only character classes such as \w or [:alpha:] to refer to letters in the alphabet but whenever I try to execute the program I'm getting an error:

grep: Invalid back reference.

Would anyone be able to shed some light on how to fix this problem? Thanks!

EDIT: my new code but now regex 3 (almost works but gives me an extra word its not supposed to) and 5 (isn't doing anything) aren't doing what they are supposed to, any help please?

#!/bin/bash

src_file="words.txt"

regex1='^(.)(.).\2\1'
regex2='^(.)(.)(.)\3\2\1'
regex3='^(.)(.)(.).\3\2\1'
regex4='(.)\1+'
regex5='^(.)\1{2}'

echo 'These are the five letter palindromes:'
egrep $regex1 $src_file

echo ' '
echo 'These are the six letter palindromes:'
egrep $regex2 $src_file

echo ' '
echo 'These are the seven letter palindromes:'
egrep $regex3 $src_file

echo ' '
echo 'These are the words that contain at least two instances of the same doubled characters (such as willfully (contains ll twice) and riffraff (contains ff twice)):'
egrep $regex4 $src_file

echo ' '
echo 'These are the words that contain at least three instances of doubled characters (such as bookkeeper  (oo, kk, and ee) and keenness (ee, nn, and ss):'
egrep $regex5 $src_file

missing.n0
  • 19
  • 3
  • 1
    Replace `egrep` with `grep` or remove all backslashes before brackets. – Cyrus Sep 30 '19 at 01:10
  • `egrep $regex3 $src_file` where `$regex3` is essentially undefined might very well generate that error message. Good luck. – shellter Sep 30 '19 at 01:23
  • @Cyrus cheers that worked, thank you! However, now I'm realizing that my regex's are wrong... I totally thought I had the 6 letter palindromes with (.)(.)(.)\3\2\1 but that isn't working and its spitting out palindromes with more than 6 letters, any idea as to why? – missing.n0 Sep 30 '19 at 01:29
  • `regex2='^\(.\)\(.\)\(.\)\3\2\1$'`? – Cyrus Sep 30 '19 at 01:31
  • See: [The Stack Overflow Regular Expressions FAQ](http://stackoverflow.com/a/22944075/3776858) – Cyrus Sep 30 '19 at 01:31
  • Regarding `regex3`, what is the *extra word* which is not supposed to? It is essentially same as `regex1`. – tshiono Sep 30 '19 at 03:42
  • Putting the regexes in variables doesn't really buy you anything, and you really should [quote your variables](/questions/10067266/when-to-wrap-quotes-around-a-shell-variable/27701642) if you do that. – tripleee Sep 30 '19 at 06:57

1 Answers1

0

Your regex3 looks almost ok, but possible overmatch will be the case with words such as wow wow or revivers. Then please try:

regex3='^(.)(.)(.)\w\3\2\1$'

To be strict, it will be safer to replace all dots with \w as:

regex3='^(\w)(\w)(\w)\w\3\2\1$'

Needless to say the anchor $ is required for regex1 and regex2.

As of regex4, it should be:

regex4='(\w)\1.*\1\1'

or

regex4='((\w)\2).*\1'

to match with willfully and riffraff.

and regex5 should be:

regex5='(\w)\1.*(\w)\2.*(\w)\3'

to match with bookkeeper and keenness.

Note that the character class \w is equivalent to [[:alnum:]]. If you want to limit the matching only to alphabets, please replace \w with [[:alpha:]].

BTW you can also check if a variable $str is a palindrome with:

[[ $str = $(rev <<< "$str") ]] && echo "$str is a palindrome"

as an alternative.

Hope this helps.

tshiono
  • 21,248
  • 2
  • 14
  • 22