正则介绍_grep用法
正则介绍 正则表达式,又称规则表达式。(英语:Regular Expression,在代码中常简写为regex、regexp或RE),计算机科学的一个概念。正则表通常被用来检索、替换那些符合某个模式(规则)的文本。 许多程序设计语言都支持利用正则表达式进行字符串操作。例如,在Perl中就内建了一个功能强大的正则表达式引擎。正则表达式这个概念最初是由Unix中的工具软件(例如sed和grep)普及开的。正则表达式通常缩写成“regex”,单数有regexp、regex,复数有regexps、regexes、regexen。 grep/egrep grep (缩写来自Globally search a Regular Expression and Print)是一种强大的文本搜索工具,它能使用特定模式匹配(包括正则表达式)搜索文本,并默认输出匹配行。 egrep 命令是一个搜索文件获得模式,使用该命令可以任意搜索文件中的字符串和符号,也可以为你搜索一个多个文件的字符串,一个提示符可以是单个字符、一个字符串、一个字、一个句子。 选项:
grep/egrep 示例: [root@localhosta]#grep-n'root'/etc/passwd显示行号 [root@localhosta]#grep-nv'nologin'/etc/passwd显示行号,筛选不含nologin字符 [root@localhosta]#grep'[0-9]'/etc/inittab//表示显示出数字 [root@localhosta]#grep-v'[0-9]'/etc/inittab不选数字 [root@localhosta]#cat1.txt 12121414 #141212414 #sdfsfefwfw sfsfsfsf 123556 [root@localhosta]#grep-v'^#'/tmp/a/1.txt显示开头不是#的行//^表示以什么开头 12121414 sfsfsfsf 123556 [root@localhosta]#grep'[^0-9]'/etc/inittab[^]表示非即显示不包含数字 [root@localhosta]#grep'[^a-zA-Z]'1.txt表示筛选数字 [root@localhosta]#grep'[r.o]'/etc/passwd.表示任意一个字符 [root@localhosta]#grep-n'oo*'/tmp/a/1.txt*表示重复左边n个字符n:0个或多个 [root@localhosta]#grep'.*'1.txt o*表示匹配左边的字符n个字符即所有的字符 ro roo roo rooo roooo rooooo [root@localhosta]#grep'user.*bash'/etc/passwd user:x:1000:1000:user:/home/user:/bin/bash [root@localhosta]#grep'o{2}'/etc/passwd//表示匹配2个o [root@localhosta]#egrep'o{2}'/etc/passwd//不加脱义符号 [root@localhosta]#egrep'o+'/etc/passwd//+表示左边的字符的重复一次或多次 [root@localhosta]#egrep'oo?'/etc/passwd?表示左边的0或者1个字符 [root@localhosta]#egrep'root|nologin'/etc/passwd|表示或 [root@localhosta]#egrep'(oo){1}'/etc/passwd (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |