Shell script to detect the input string

Source: Internet
Author: User

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


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.