9.1 Regular Introduction grep (UP)
什么是正则 正则就是一串有规律的字符串 掌握好正则对于编写shell脚本有很大帮助 各种编程语言中都有正则,原理是一样的 本章主要学习 grep/egrep、sed、awk掌握规律grep grep [-cinvABC] ‘work‘ filename -c 行数 -i 不区分大小写 -n 显示行号 -v 取反 -r 遍及所有子目录 -A 后面跟数字,过滤出符合要求的行以及下面n行 -B 后面跟数字,过滤出符合要求的行以及上面n行 -C 后面跟数字,同时过滤出符合要求的行以及上下各n行 [[email protected] grep]# grep -c ‘nologin‘ passwd 16 [[email protected] grep]# grep -n ‘nologin‘ passwd [[email protected] grep]# grep -i -c ‘nologin‘ passwd 16 [[email protected] grep]# grep -v -c ‘nologin‘ passwd 4 [[email protected] grep]# grep -vc ‘nologin‘ passwd 4 [[email protected] grep]# grep -v ‘nologin‘ passwd root:x:0:0:root:/root:/bin/bash sync:x:5:0:sync:/sbin:/bin/sync shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown halt:x:7:0:halt:/sbin:/sbin/halt [[email protected] grep]# grep -A2 ‘root‘ passwd
9.2 grep (middle)
grep/egrep example grep-n ' root '/etc/passwd grep-nv ' nologin '/etc/passwd grep ' [0-9] '/etc/ininttab grep-v ' [0 -9] '/etc/inittab grep-v ' ^# ' inittab grep-v ' ^# ' inittab|grep-v ' ^$ ' grep ' ^[^a-za-z] ' test.txt grep ' r.o ' Test.txt grep ' oo ' test.txt grep '. * ' test.txt grep ' o\{2\} '/etc/passwd egrep ' o{2} '/etc/passwd egrep ' O + '/etc/passwd egrep ' oo? '/etc/passwd egrep ' root|nologin '/etc/passwd egrep ' (oo) {2} '/etc/passwd [Emai L protected] grep]# grep-n ' ^# ' Inittab 1:# Inittab is the no longer used when using SYSTEMD. 2:# 3:# ADDING CONFIGURATION Here is the NO EFFECT on YOUR SYSTEM. 4:# 5:# Ctrl-alt-delete is handled by/usr/lib/systemd/system/ctrl-alt-del.target 6:# 7:# systemd use S ' targets ' instead of runlevels. By default, there is both main targets:8:# 9:# multi-user.target:analogous to runlevel 3 10:# graph Ical.target:analogous to RunLevel 5 11:# 12:# to view current default target, run:16:# systemctl get-default 17:# 18:# to SE t a default target, run:19:# systemctl set-default target.target 20:# [[email protected] grep]# Grep-nv ' ^# ' inittab 13:adfafsafd 14:ssss 15:66666 [[email protected] grep]# grep-n ' [^0-9] ' Inittab 1:# inittab is no longer used when using SYSTEMD. 2:# 3:# ADDING CONFIGURATION Here is the NO EFFECT on YOUR SYSTEM. 4:# 5:# Ctrl-alt-delete is handled by/usr/lib/systemd/system/ctrl-alt-del.target 6:# 7:# systemd use S ' targets ' instead of runlevels. By default, there is maintargets:8:# 9:# multi-user.target:analogous to runlevel 3 10:# Graphi Cal.target:analogous to RunLevel 5 11:# 12:# to view current default target, RUN:13:ADFAFSAFD 14:ssss 16:# systemctl get-default 17:# 18:# To Set a default target, run:19:# systemctl set-default target.target 20:# [[email protected] Gr ep]# grep-n ' ^[^0-9] ' inittab 1:# inittab are no longer used when using SYSTEMD. 2:# 3:# ADDING CONFIGURATION Here is the NO EFFECT on YOUR SYSTEM. 4:# 5:# Ctrl-alt-delete is handled by/usr/lib/systemd/system/ctrl-alt-del.target 6:# 7:# systemd use S ' targets ' instead of runlevels. By default, there is both main targets:8:# 9:# multi-user.target:analogous to runlevel 3 10:# graph Ical.target:analogous to RunLevel 5 11:# 12:# to view current default target, RUN:13:ADFAFSAFD 14:ssss 16:# systemctl get-default 17:# 18:# to set a default target, run:19:# systemctl s Et-default target.target 20:# [[email protected] grep]# grep-nv ' ^[^0-9] ' inittab 15:66,666
9.3 grep (bottom)
. Match. Any one of the characters in each of the two words * repeat * the character on the left 0 or countless + repeat + left 1 to countless? Repeat? 0 or 1 times of the preceding character | or () Find out the identification of the group string () + multiple repeating group strings [[email protected] grep]# grep ' r.o ' passwd root:x:0:0:root:/root:/bin/bash o Perator:x:11:0:operator:/root:/sbin/nologin [[email protected] grep]# grep ' {2} ' passwd [[email protected ] grep]# grep ' o{2} ' passwd [[email protected] grep]# grep ' o\{2\} ' passwd Root:x:0:0:root:/root:/bin/bash Lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin Mail:x:8:12:mail:/var/spool/mail:/sbin/nologin Operator:x:11:0:operator :/root:/sbin/nologin postfix:x:89:89::/var/spool/postfix:/sbin/nologin Bacula:x:133:133:bacula Backup System:/var/ Spool/bacula:/sbin/nologin grep-e ' o{2} ' passwd = = Egrep ' o{2} ' passwd [[email protected] grep]# egrep ' o+ O ' passwd [[email protected] grep]# grep-e ' root|nologin ' passwd [[email protected] data]# grep-r--INCL Ude= "*.php" ' eval '/tmp/data/tmp/data/ext.php:eval
2018-03-12 Amin Linux Learning