shell脚本实现检測回文字符串
shell 实现 字符串 脚本 回文
2023-09-14 09:07:56 时间
全部回文字的结构特征例如以下:
假设字符数是偶数,那么它在结构上表现为:一个字符序列连着还有一个字符同样但次序恰好相反的字符序列。
假设字符数为奇数,那么它在结构上表现为:一个字符序列连着还有一个字符同样但次序恰好相反的字符序列,可是这两个序列中间共享一个同样的字符。
sed命令可以记住之前匹配的子样式。
可以用正則表達式:'\(.\)'。匹配随意一个字符。\1表示其反向引用。如匹配有两个字符的回文正則表達式为:
'\(.\)\(.\)\2\1'
匹配随意长度的回文脚本例如以下所看到的:
#!/bin/bash #file name: match_palindrome.sh #function: find palindrome in a file. if [ $# -ne 2 ] then echo "Usage: $0 filename string_length" exit -1 fi filename=$1 basepattern='/^\(.\)' count=$(( $2/2 )) # matche certain length for ((i=1; i < $count; i++)) do basepattern=$basepattern'\(.\)'; done # the length is even if [ $(( $2 % 2)) -ne 0 ] then basepattern=$basepattern'.'; fi for ((count; count > 0; count--)) do basepattern=$basepattern'\'"$count"; done echo "debug: $basepattern" # print the result basepattern=$basepattern'$/p' sed -n "$basepattern" $filename
相关文章
- 【shell 】高亮输出显示
- shell脚本实现Hbase服务的监控报警和自动拉起
- ERROR [org.apache.hadoop.util.Shell] - Failed to locate the winutils binary in the hadoop binary path
- Shell - 简明Shell入门10 - 管道(Pipe)
- Linux Shell脚本自动化编程实战- shell并发
- shell实现洗牌随机
- 批量建用户的shell小脚本
- shell实现洗牌随机
- Shell 四剑客sed 之生产环境上,最常用的一套“Sed“技巧
- Linux cat EOF在shell脚本里面的使用详解
- Linux Shell脚本联动iptables实现DOS防护
- Linux 使用Shell脚本实现测试网络中哪些服务器可以ping通
- shell 字符串截取
- Shell脚本[运算表达式,条件控制语句]
- shell case命令
- 监控docker容器运行状态脚本shell
- 反弹shell的实现方式和检测方法——常规攻击可以直接检测,pipe方式需要结合关联分析(图关联最好),如果含有混淆脚本,需要无文件攻击检测
- shell 脚本接收参数
- shell脚本学习:weblogic中间件domain域实现自动化备份
- shell中利用sed命令去掉文本中的单引号(‘)
- Jenkins调用shell命令实现持续集成(四)
- shell脚本配合zabbix实现tomcat的故障自愈