Sed command to print palindrome

Consider the following file called palindrome containing some 6-letter words, some of them palindromes (i.e., words that read the same backwards and forwards) and some not:

palindrome
toyota
pullup
abccba
nissan
redder

Write a one-line sed command that prints out only the lines containing 6-letter palindromes so that after running the appropriate sed command, the following would be output to the terminal:

pullup
abccba
redder

Here, I will use -E option so that I don't have to escape parenthesis and sed command looks better. In the question, it is mentioned that 6-letter words are provided and for 6-letter word to become palindrome the first three characters must be written in the reverse order as the last three characters in that word. p command is used for printing the words along with -n option to suppress unnecessary lines.

$sed -E -n '/^(.)(.)(.)\3\2\1$/p' palindrome

Recommended Posts