grep and regular expressions

grep and regular expressions

1, grep command format

grep [选项] pattern filename filename.....
 # grep 'Tom' /etc/passwd
 # grep ‘bash shell’ /etc/passwd
找到:          grep返回的退出状态为0
没找到:        grep返回的退出状态为1
找不到指定文件:grep返回的退出状态为2

grep程序的输入可以来自标准输入或管道,而不仅仅是文件,例如:
# grep 'tom'
# ps -aux | grep 'sshd'
# ll | grep '^d'   #过滤目录
# egrep ‘^root’ /etc/passwd /etc/shadow /etc/group

2, grep option

-i --ignore-case    忽略大小写
-l --file-with-matches  只列出匹配行所在的文件名
-n --line-number    在每一行前面加上它在文件中的相对行
-c --count          显示成功匹配的行数
-s --no-messages    禁止显示文件中不存在或文件不可读错误
-q --quiet,--silent 静默 --quiet    grep -q 'root' /etc/passwd; echo $? 不输出结果
-v --invert-mach     反向查找,志向是不匹配的行
-R,-r  --recursive   递归针对目录
--color              颜色
-o --only-matching   只显示匹配的内容,而不是一整行
-B --before-context=NUM 输出前面几行的内容
-A --after-contest=NUM  输出后面几行的内容
-C --context=NUM        输出上下文几行的内容
-B -A -C 例如:
grep -A2 'root' /etc/passwd    #输出带root后两行
grep -B2 ‘test’ /etc/passwd    #输出带test前两行
grep -C2 'alice' /etc/passwd   #输出带alice上下两行
useradd --help | grep -C2 "\-c" 同上

3, grep using metacharacters

grep使用的基本元字符集: ^  $  .  *    []  [^]   \<\>   \(\)  \+  \|
^以什么开头
$以什么结尾
. 任意单个字符
* 匹配前面字符0~多次
[] 匹配其中的内容
[^] 不匹配其中的内容
\<\> 词首词尾定界符
\(\) 括号中的内容为一个组
\+ 匹配前面内容1~多次
 \|  或的关系

4, extended use of meta-characters grep

egrep(或grep -E):使用扩展元字符? + {} | ()
? 匹配0-1次 
匹配1-n次 
{n} 匹配前导内容多次 
| 或者的意思 
()其中的内容为一个组

5, grep example

ps: not sure or do not want to remember whether to expand meta-characters or metacharacters, can be directly used egrep, a wildcard.

#用/etc/passwd的前10行举例
[root@localhost ~]# head -10 /etc/passwd > passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin


Filter rows with the root
[root @ localhost ~] # egrep 'root' the passwd
root: X: 0: 0: root: / root: / bin / the bash
operator: X:. 11: 0: operator: / root: / sbin / nologin


P * from the filter with a root file fields
[root @ localhost ~] # egrep 'root' p *
the passwd: root: X: 0: 0: root: / root: / bin / the bash
the passwd: operator: X:. 11 : 0: operator: / root: / sbin / nologin


Filtered beginning with line r
[the root @ localhost ~] # egrep '^ r' the passwd
the root: X: 0: 0: the root: / the root: / bin / the bash


Sh with the end of the line filter
[the root @ localhost ~] # egrep 'sh $' the passwd
the root: X: 0: 0: the root: / the root: / bin / the bash


The filter has a character of any subsequent line, \ escape behind 5:00
[the root @ localhost ~] # echo "Sync: X: 5.0aa" >> the passwd
[the root @ localhost ~] # egrep '. 5 .. 'the passwd
Sync: X: 5.0aa


Filtration amd the row beginning with
[the root @ localhost ~] # egrep '^ [amd]' the passwd
daemon: X: 2: 2: daemon: / sbin: / sbin / nologin
ADM: X:. 3:. 4: ADM: / var / ADM: / sbin / nologin
mail: X:. 8: 12 is: mail: / var / spool / mail: / sbin / nologin


Matching lines, in full compliance with the contents of the mixing is not removed
[the root @ localhost ~] # egrep '[^ the root: X: 0: 0: the root: / the root: / bin / the bash]' the passwd
bin: X:. 1:. 1 : bin: / bin: / sbin / nologin
daemon: X: 2: 2: daemon: / sbin: / sbin / nologin
ADM: X:. 3:. 4: ADM: / var / ADM: / sbin / nologin
LP: X: . 4:. 7: LP: / var / spool / LPD: / sbin / nologin
......


0-9 with matching rows
[the root @ localhost ~] # egrep '[0-9]' the passwd
the root: X: 0: 0: the root: / the root: / bin / the bash
bin: X:. 1:. 1: bin : / bin: / sbin / nologin
........


Match lines starting at 0-9
[root @ localhost ~] # egrep '^ [0-9]' passwd


Ro or r with matching lines of
[the root @ localhost ~] # egrep 'ro *' the passwd
the root: X: 0: 0: the root: / the root: / bin / the bash
ADM: X:. 3:. 4: ADM: / var / ADM: / sbin / nologin
LP: X:. 4:. 7: LP: / var / spool / LPD: / sbin / nologin
mail: X:. 8: 12 is: mail: / var / spool / mail: / sbin / nologin
operator : x: 11: 0: operator : / root: / sbin / nologin


0-9 match occurs at least three times the line
[the root @ localhost ~] # echo '111111' >> the passwd
[the root @ localhost ~] # echo '2222222' >> the passwd
[the root @ localhost ~] # echo '3333333'> > the passwd
[the root @ localhost ~] # egrep '^ [0-9] {}. 6' the passwd
111111
2222222
3333333


Filtering the first word sync for the ending line, can be used apart \ <first word \> suffix
[the root @ localhost ~] # egrep '\ <sync \>' the passwd
sync: X:. 5: 0: sync: / sbin: / bin / Sync
Sync: the X-: 5.0aa


Filtering the first word of the group (Shut), and the intermediate multiple match any character, the last row of n
[the root @ localhost ~] # egrep '\ <(Shut) * n.' The passwd
the shutdown: X:. 6: 0: shutdown: / sbin: / sbin / shutdown


Filtration or shut lines with a root
[root @ localhost ~] # egrep 'root | shut' the passwd
root: X: 0: 0: root: / root: / bin / the bash
the shutdown: X:. 6: 0: the shutdown: / sbin: / sbin / the shutdown
operator: X:. 11: 0: operator: / the root: / sbin / nologin


Matching rows with 1 to 3 times
[the root @ localhost ~] # egrep '3+' the passwd
ADM: X: 3:. 4: ADM: / var / ADM: / sbin / nologin
3333333


Filter behind r is a or o line
[the root @ localhost ~] # egrep 'r (a | o)' the passwd
the root: X: 0: 0: the root: / the root: / bin / the bash
operator: X:. 11: 0 : operator: / root: / sbin / nologin


SH or filter rows with h,
[the root @ localhost ~] # echo "SHUTDOWN ....." >> the passwd
[the root @ localhost ~] # egrep 'sH | h' the passwd
the root: X: 0: 0: the root : / the root: / bin / the bash
the shutdown: X:. 6: 0: the shutdown: / sbin: / sbin / the shutdown
HALT: X:. 7: 0: HALT: / sbin: / sbin / HALT
SHUTDOWN .....


R ro filtration or after a 0-1 times a character appears in the row
[the root @ localhost ~] # egrep the passwd 'ro?'
The root: X: 0: 0: the root: / the root: / bin / the bash
ADM: X:. 3: . 4: ADM: / var / ADM: / sbin / nologin
LP: X:. 4:. 7: LP: / var / spool / LPD: / sbin / nologin
mail: X:. 8: 12 is: mail: / var / spool / mail : / sbin / nologin
operator: X:. 11: 0: operator: / the root: / sbin / nologin

Guess you like

Origin blog.51cto.com/13760226/2411250