1.1字符1.1.1特定字符:''包含“root”字符[niewj@niewj~]$grep'root'passwdroot:x:0:0:root:/root:/bin/bash1.1.2Rangecharacters:[]charactersincludingnumbers+大小写字母grep'[0-9a-zA-Z]'passwd1.1.3Anycharacter.[niewj@niewj~]$grep'.'passwd都会被过滤掉1.1.4负[^][niewj@niewj~]$grep'[^a-zA-Z0-9:/-]'passwd不包含大小写字母,不包含数字,不包含冒号,不包含斜杠的结果,不包含破折号。1.2边界字符^$1.2.1第一个字符^和最后一个字符$以ftp开头[niewj@niewj~]$grep'^ftp'passwdftp:x:14:50:FTPUser:/var/ftp:/sbin/nologin以false结尾[niewj@niewj~]$grep'false$'passwdmysql:x:27:27:MySQLServer:/var/lib/mysql:/bin/false空行:'^$'[niewj@niewj~]$grep'^$'passwd1.3元字符\w\W1.3.1字母数字下划线\w等价于grep'[a-zA-Z0-9_]'passwd[niewj@niewj~]$grep'\w'passwd1.3.1非字母数字下划线\W等价于grep'[^a-zA-Z0-9_]'passwd[niewj@niewj~]$grep'\W'passwd1.4分词元字符\b[niewj@niewj~]$grep'\bx\b'passwd//这样不会过滤掉这样的行:/var/spool/postfix:/sbin/nologin1.5重复的字符代表1.5.1的字符串,普通的字符串,比如:'root'、'r..t'等;过滤两个相连的数字,错误方式:[niewj@niewj~]$grep'0-9'passwd正确方式:添加单词分隔符[niewj@niewj~]$grep'\b[0-9][0-9]\b'passwd1.5.2重复字符表示*零次或多次+一次或多次?创建文本文件零次或一次:test.txtniewjhaohellonihaohellonieehaohelloniewjniceniewjabcnilsonniewjniewjniewjniniiiininininininijksdf;ni;1.5.3单个字符重复#1。包含“nie”,而“e”有0个或多个[niewj@niewj~]$grep'nie*'test.txtniewjhaohellonihaohellonieehaohelloniewjniceniewjabcnilsonniewjniewjniewjniniiiininininininij;ni#2。包含“nie”,且有一个或多个“e”,(注意:“+”前必须有转义符\)[niewj@niewj~]$grep'nie\+'test.txtniewjhaohellonieehaohelloniewjniceniewjniewjniewjniewj#3。包含“nie”,“e”有0或1(注意:“?”前必须有转义符\)[niewj@niewj~]$grep'nie\?'test.txtniewjhaohellonihaohellonieehaohelloniewjniceniewjabcnilsonniewjniewjniewjniniiiininininininij;ni(注:“?”前加转义符)(注:“+”前加转义符)1.5.4一组字符重复#1。包含“nie”,并且“nie”有0个或多个[niewj@niewj~]$grep'\(nie\)*'test.txt//稍微因为0将匹配空行和所有#2。包含“nie”,并且“nie”有一个或多个[niewj@niewj~]$grep'\(nie\)\+'test.txtniewjhaohellonieehaohelloniewjniceniewjniewjniewjniewj#3。包含“nie”,“nie”有0个或1个[niewj@niewj~]$grep'\(nie\)\?'test.txt//省略。因为0会匹配空行和所有1.5.5重复限制{n,m}*{0,}+{1,}?{0,1}#"niewj"这个字符串出现了1-2次[niewj@niewj~]$grep'\(niewj\)\{1,2\}'test.txtniewjhaohelloniewjniceniewjniewjniewjniewj在这里你会发现“niewjniewjniewj”其实是3次,但是也列出来了,这是常规的“贪心匹配模式”会进行多次匹配。1.5.6注意任意字符串的贪心模式#如果我们要找的是“niewj”,下面的是错误的,因为它实际上匹配的是:“niewj:x:1000:1000::/home/niewj”。niewj@niewj~]$grep'n.*j'passwdniewj:x:1000:1000::/home/niewj:/bin/bash#下面的匹配是正确的:2"niewj"[niewj@niewj~]$grep'\bn[a-zA-Z]*j\b'passwdniewj:x:1000:1000::/home/niewj:/bin/bash#下面两个也可以:#grep'n[a-zA-Z]*j'passwd#grep'n\w*j'passwdgrep'n.*j'passwd匹配要注意贪心模式,比如上面获取"niewj"不能用".*"1.6逻辑或匹配|文本有包含“bin/false”的行,也有包含“bin/sync”的行,如果两者都需要,使用逻辑或“|”[niewj@niewj~]$grep'bin/\(false\|sync\)'passwdsync:x:5:0:sync:/sbin:/bin/syncmysql:x:27:27:MySQLServer:/var/lib/mysql:/bin/false上面的例子其实表示:“bin/(false|sync)”因为括号和“|”需要转义,添加转义字符。1.7几个案例1.7.1匹配4-10位的QQ号测试数据:qq.txtasdkfjjd;k578374174375893274958ad8485883a858512111319890202124X123456789012345123456789012345x12345678901234x345679876556890967679基本规则:0-94-10位数字开头数字结尾所以是:grep'^[0-9]\{4,10\}$'qq.txt[niewj@niewj~]$grep'^[0-9]\{4,10\}$'qq.txt1743758932749581.7.2MatchIDnumber(15-18andsupportXtail)basicrules:thefirstdigitcannotbe0,themiddleis13-16digits,themantissaisanumberor"X"or"x"[niewj@niewj~]$grep'^[1-9][0-9]\{13,16\}[0-9xX]$'qq.txt12111319890202124X123456789012345123456789012345x12345678901234x3456798765568909671.7.3匹配密码规则数字、字母、下划线组成、大于8位[niewj@niewj~]$grep'^\w\{8,\}$'qq.txt174375893883a858512111319890202124X123456789012345123456789012345x12345678901234x345679876556890967
