The structure features of all return texts are as follows:
If the number of characters is an even number, it is structured as: A Character Sequence is connected to another character sequence with the same but the opposite order.
If the number of characters is odd, it is structured as: A Character Sequence is connected to another character sequence with the same but the opposite order, but the two sequences share the same character.
The SED command can remember the previously matched child styles. You can use the regular expression '\ (. \)' to match any character. \ 1 indicates its reverse reference. If the regular expression that matches two characters in the background is:
'\ (. \) \ (. \) \ 2 \ 1'
The script that matches any length of input is as follows:
#!/bin/bash#file name: match_palindrome.sh#function: find palindrome in a file.if [ $# -ne 2 ] thenecho "Usage: $0 filename string_length"exit -1fifilename=$1basepattern='/^\(.\)'count=$(( $2/2 ))# matche certain length for ((i=1; i < $count; i++))dobasepattern=$basepattern'\(.\)';done# the length is evenif [ $(( $2 % 2)) -ne 0 ]thenbasepattern=$basepattern'.';fifor ((count; count > 0; count--))dobasepattern=$basepattern'\'"$count";doneecho "debug: $basepattern"# print the resultbasepattern=$basepattern'$/p'sed -n "$basepattern" $filename