shell脚本文件名:match_palindrome.sh
内容:
[zhang@zhangshibo temp]$ vi match_palindrome.sh
if [ $# -ne 2 ];
then
echo "Usage: $0 filename string_length"
exit -1
fi
filename=$1;
basepattern='/^\(.\)'
count=$(( $2 / 2))
for((i=1;i<$count;i++))
do
basepattern=$basepattern'\(.\)';
done
if [ $(( $2 % 2 )) -ne 0 ];
then
basepattern=$basepattern'.';
fi
for((count;count>0;count--))
do
basepattern=$basepattern'\'"$count";
done
basepattern=$basepattern'$/p'
sed -n "$basepattern" $filename
脚本使用方法:
加入我用一个文本文件:words.txt
其中文本文件内容如下:
used this used used this counting
noon
peep
poop
sees
zhang
shi
bo
hu
xia
shu
那么:
[zhang@zhangshibo temp]$ ./match_palindrome.sh words.txt 4
noon
peep
poop
sees