源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

Linux Shell脚本的编程之正则表达式

  • 时间:2020-03-08 23:24 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:Linux Shell脚本的编程之正则表达式
[b]一 正则表达式与通配符[/b] 1 正则表达式是用在文件中匹配符合条件的字符串,正则是包含匹配,grep,awk,sed等命令可以支持正则表达式 2 通配符是用来匹配符合条件的文件名,通配符是完全匹配,ls,find,cp这些命令不支持正则表达式,所以只能用Shell自己的通配符来进行匹配了。 [b]二 基础正则表达式[/b] 这里引用兄弟连的测试文本 [img]http://files.jb51.net/file_images/article/201611/2016111610250230.png[/img]   1 * 前一个字符匹配0次或任意多次 grep “a*” test_rule.txt 匹配所有内容,包括空白行(由于*可以匹配0次) [img]http://files.jb51.net/file_images/article/201611/2016111610250231.png[/img] grep “aa*” test_rule.txt 匹配至少包含有一个a的行 [img]http://files.jb51.net/file_images/article/201611/2016111610250232.png[/img] grep “aaa*” test_rule.txt 匹配至少包含有两个a的行 [img]http://files.jb51.net/file_images/article/201611/2016111610250233.png[/img] 2 . 匹配除了换行符之外的任意一个字符 grep “s..d” test_rule.txt 匹配s和d直接一定要有两个字符的行 [img]http://files.jb51.net/file_images/article/201611/2016111610250334.png[/img] grep “s.*d” test_rule.txt 匹配s和d直接任意字符 [img]http://files.jb51.net/file_images/article/201611/2016111610250335.png[/img] grep “.*” test_rule.txt 匹配所有内容 [img]http://files.jb51.net/file_images/article/201611/2016111610250336.png[/img] 3 ^匹配行首 $匹配行尾 grep “^M” test_rule.txt 匹配以大写M开头的行 [img]http://files.jb51.net/file_images/article/201611/2016111610250337.png[/img] grep “n$” test_rule.txt 匹配以小写n结尾的行 [img]http://files.jb51.net/file_images/article/201611/2016111610250338.png[/img] grep -n “^$” test_rule.txt 匹配空白行 [img]http://files.jb51.net/file_images/article/201611/2016111610250339.png[/img] 4 [] 匹配括号中指定的任意一个字符,只匹配一个字符 grep “s[ao]id” test_rule.txt 匹配s和i字母之间,要么是a,要么是o的行 [img]http://files.jb51.net/file_images/article/201611/2016111610250340.png[/img] grep “[0-9]” test_rule.txt 匹配任意一个数字 [img]http://files.jb51.net/file_images/article/201611/2016111610250341.png[/img] grep “^[a-z]” test_rule.txt 匹配用小写字母开头的行 [img]http://files.jb51.net/file_images/article/201611/2016111610250342.png[/img] 5 [^] 匹配除括号中以外的任意一个字符 grep “^[^a-z]” test_rule.txt 匹配不用小写字母开头的行 [img]http://files.jb51.net/file_images/article/201611/2016111610250343.png[/img] grep “^[^a-zA-Z]” test_rule.txt 匹配不用字符开头的行 [img]http://files.jb51.net/file_images/article/201611/2016111610250344.png[/img] 6 “\” 转义符 grep “.$” test_rule.txt 匹配以.结尾的行 [img]http://files.jb51.net/file_images/article/201611/2016111610250345.png[/img] 7 {n} 表示其前面的字符恰好出现n次 grep “a{3}” test_rule.txt 匹配字母a连续出现3次的行 [img]http://files.jb51.net/file_images/article/201611/2016111610250346.png[/img] grep “[0-9]{3}” test_rule.txt 匹配包含连续3个数字的行 [img]http://files.jb51.net/file_images/article/201611/2016111610250347.png[/img] 8 {n,} 表示其前面的字符出现不小于n次 grep “^[0-9]{3,}” test_rule.txt 匹配至少连续3个数字开头的行 [img]http://files.jb51.net/file_images/article/201611/2016111610250348.png[/img] 9 {n,m} 表示其前面的字符出现不小于n次,最多m次 grep “sa{1,3}i” test_rule.txt 匹配s和i之间最少1个a,最多3个a [img]http://files.jb51.net/file_images/article/201611/2016111610250349.png[/img] 好了,Linux Shell编程-正则表达式就总结到这里,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对编程素材网网站的支持!
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部