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