2018-1-15 6周1次课 grep
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了2018-1-15 6周1次课 grep相关的知识,希望对你有一定的参考价值。
9.1 正则介绍_grep(上)
·正则就是一串有规律的字符串
·掌握好正则对于编写shell脚本有很大帮助
·各种编程语言中都有正则,原理是一样的
·本章将要学习grep/egrep、sed、awk
grep [-cinvABC] 'word' filename
-c 行数
-i 不区分大小写
-n 显示行号
-v 取反
-r 遍历所有子目录
-A 后面跟数字,过滤出符合要求的行以及下面n行
-B 同上,过滤出符合要求的行以及上面n行
-C 同上,同时过滤出符合要求的行以及上下各n行
[[email protected] ~]# mkdir grep [[email protected] ~]# cd grep/ [[email protected]ocalhost grep]# cp /etc/passwd . [[email protected] grep]# ls passwd [[email protected] grep]# grep 'nologin' 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 mail:x:8:12:mail:/var/spool/mail:/sbin/nologin operator:x:11:0:operator:/root:/sbin/nologin games:x:12:100:games:/usr/games:/sbin/nologin ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin nobody:x:99:99:Nobody:/:/sbin/nologin systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin dbus:x:81:81:System message bus:/:/sbin/nologin polkitd:x:999:997:User for polkitd:/:/sbin/nologin postfix:x:89:89::/var/spool/postfix:/sbin/nologin sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin chrony:x:998:996::/var/lib/chrony:/sbin/nologin [[email protected] grep]# grep -c 'nologin' passwd ##-c 显示行数 15 [[email protected] grep]# grep -n 'nologin' passwd ##-n 显示行号 2:bin:x:1:1:bin:/bin:/sbin/nologin 3:daemon:x:2:2:daemon:/sbin:/sbin/nologin 4:adm:x:3:4:adm:/var/adm:/sbin/nologin 5:lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin 9:mail:x:8:12:mail:/var/spool/mail:/sbin/nologin 10:operator:x:11:0:operator:/root:/sbin/nologin 11:games:x:12:100:games:/usr/games:/sbin/nologin 12:ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin 13:nobody:x:99:99:Nobody:/:/sbin/nologin 14:systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin 15:dbus:x:81:81:System message bus:/:/sbin/nologin 16:polkitd:x:999:997:User for polkitd:/:/sbin/nologin 17:postfix:x:89:89::/var/spool/postfix:/sbin/nologin 18:sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin 19:chrony:x:998:996::/var/lib/chrony:/sbin/nologin [[email protected] grep]# vim passwd ##vim中把第二行nologin改为NOLogin [[email protected] grep]# grep -n 'nologin' passwd ##大写NOLogin就无法识别 3:daemon:x:2:2:daemon:/sbin:/sbin/nologin 4:adm:x:3:4:adm:/var/adm:/sbin/nologin 5:lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin 9:mail:x:8:12:mail:/var/spool/mail:/sbin/nologin 10:operator:x:11:0:operator:/root:/sbin/nologin 11:games:x:12:100:games:/usr/games:/sbin/nologin 12:ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin 13:nobody:x:99:99:Nobody:/:/sbin/nologin 14:systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin 15:dbus:x:81:81:System message bus:/:/sbin/nologin 16:polkitd:x:999:997:User for polkitd:/:/sbin/nologin 17:postfix:x:89:89::/var/spool/postfix:/sbin/nologin 18:sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin 19:chrony:x:998:996::/var/lib/chrony:/sbin/nologin [[email protected] grep]# grep -in 'nologin' passwd ##-i 不区分大小写,可识别第二行NOLogin 2:bin:x:1:1:bin:/bin:/sbin/NOLogin 3:daemon:x:2:2:daemon:/sbin:/sbin/nologin 4:adm:x:3:4:adm:/var/adm:/sbin/nologin 5:lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin 9:mail:x:8:12:mail:/var/spool/mail:/sbin/nologin 10:operator:x:11:0:operator:/root:/sbin/nologin 11:games:x:12:100:games:/usr/games:/sbin/nologin 12:ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin 13:nobody:x:99:99:Nobody:/:/sbin/nologin 14:systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin 15:dbus:x:81:81:System message bus:/:/sbin/nologin 16:polkitd:x:999:997:User for polkitd:/:/sbin/nologin 17:postfix:x:89:89::/var/spool/postfix:/sbin/nologin 18:sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin 19:chrony:x:998:996::/var/lib/chrony:/sbin/nologin [[email protected] grep]# grep -inv 'nologin' passwd ##-v 取反,把不含nologin的行过滤出来 1:root:x:0:0:root:/root:/bin/bash 6:sync:x:5:0:sync:/sbin:/bin/sync 7:shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown 8:halt:x:7:0:halt:/sbin:/sbin/halt [[email protected] grep]# grep -r 'Streaming' /etc/##-r 遍历/etc/目录下,包含Streaming的行 /etc/services:nmsp 537/tcp # Networked Media Streaming Protocol /etc/services:nmsp 537/udp # Networked Media Streaming Protocol /etc/services:h263-video 2979/tcp # H.263 Video Streaming /etc/services:h263-video 2979/udp # H.263 Video Streaming /etc/services:daqstream 7411/tcp # Streaming of measurement /etc/services:daqstream 7411/udp # Streaming of measurement /etc/services:hp-pdl-datastr 9100/udp pdl-datastream # PDL Data Streaming Port /etc/services:bmdss 13823/tcp # Blackmagic Design Streaming Server 匹配到二进制文件 /etc/udev/hwdb.bin [[email protected] grep]# grep -r 'root' /etc/ > grep.txt | grep 'passwd' grep.txt /etc/passwd:root:x:0:0:root:/root:/bin/bash /etc/passwd:operator:x:11:0:operator:/root:/sbin/nologin /etc/passwd-:root:x:0:0:root:/root:/bin/bash /etc/passwd-:operator:x:11:0:operator:/root:/sbin/nologin /etc/postfix/main.cf:# the system passwd file in the chroot jail is just not practical. [[email protected] grep]# grep -nA2 'root' passwd ##-A2 列出符合要求的行以及下面2行 1:root:x:0:0:root:/root:/bin/bash 2-bin:x:1:1:bin:/bin:/sbin/NOLogin 3-daemon:x:2:2:daemon:/sbin:/sbin/nologin -- 10:operator:x:11:0:operator:/root:/sbin/nologin 11-games:x:12:100:games:/usr/games:/sbin/nologin 12-ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin [[email protected] grep]# grep -nB2 'root' passwd ##-B2 列出符合要求的行以及上面2行 1:root:x:0:0:root:/root:/bin/bash -- 8-halt:x:7:0:halt:/sbin:/sbin/halt 9-mail:x:8:12:mail:/var/spool/mail:/sbin/nologin 10:operator:x:11:0:operator:/root:/sbin/nologin [[email protected] grep]# grep -nC2 'root' passwd ##-C2 列出符合要求的行以及上下2行 1:root:x:0:0:root:/root:/bin/bash 2-bin:x:1:1:bin:/bin:/sbin/NOLogin 3-daemon:x:2:2:daemon:/sbin:/sbin/nologin -- 8-halt:x:7:0:halt:/sbin:/sbin/halt 9-mail:x:8:12:mail:/var/spool/mail:/sbin/nologin 10:operator:x:11:0:operator:/root:/sbin/nologin 11-games:x:12:100:games:/usr/games:/sbin/nologin 12-ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
9.2 grep(中)
grep/egrep 示例
[[email protected] grep]# grep -n 'root' /etc/passwd##显示行号 1:root:x:0:0:root:/root:/bin/bash 10:operator:x:11:0:operator:/root:/sbin/nologin [[email protected] grep]# grep -nv 'root' /etc/passwd##显示行号,并且取反 2:bin:x:1:1:bin:/bin:/sbin/nologin 3:daemon:x:2:2:daemon:/sbin:/sbin/nologin 4:adm:x:3:4:adm:/var/adm:/sbin/nologin 5:lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin 6:sync:x:5:0:sync:/sbin:/bin/sync 7:shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown 8:halt:x:7:0:halt:/sbin:/sbin/halt 9:mail:x:8:12:mail:/var/spool/mail:/sbin/nologin 11:games:x:12:100:games:/usr/games:/sbin/nologin 12:ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin 13:nobody:x:99:99:Nobody:/:/sbin/nologin 14:systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin 15:dbus:x:81:81:System message bus:/:/sbin/nologin 16:polkitd:x:999:997:User for polkitd:/:/sbin/nologin 17:postfix:x:89:89::/var/spool/postfix:/sbin/nologin 18:sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin 19:chrony:x:998:996::/var/lib/chrony:/sbin/nologin [[email protected] grep]# grep '[0-9]' /etc/inittab ##含有数字的行 # multi-user.target: analogous to runlevel 3 # graphical.target: analogous to runlevel 5 [[email protected] grep]# grep -v '[0-9]' /etc/inittab ##不含有数字的行 # inittab is no longer used when using systemd. # # ADDING CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM. # # Ctrl-Alt-Delete is handled by /usr/lib/systemd/system/ctrl-alt-del.target # # systemd uses 'targets' instead of runlevels. By default, there are two main targets: # # # To view current default target, run: # systemctl get-default # # To set a default target, run: # systemctl set-default TARGET.target # [[email protected] grep]# grep -n '^#' /etc/inittab ##以#开头的行 1:# inittab is no longer used when using systemd. 2:# 3:# ADDING CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM. 4:# 5:# Ctrl-Alt-Delete is handled by /usr/lib/systemd/system/ctrl-alt-del.target 6:# 7:# systemd uses 'targets' instead of runlevels. By default, there are two main targets: 8:# 9:# multi-user.target: analogous to runlevel 3 10:# graphical.target: analogous to runlevel 5 11:# 12:# To view current default target, run: 13:# systemctl get-default 14:# 15:# To set a default target, run: 16:# systemctl set-default TARGET.target 17:# [[email protected] grep]# vim inittab ##加入一段胡乱的文字
[[email protected] grep]# grep -v '^#' inittab ##不以#开头的行 &&*&*[]%% 999999999 aaaaaa jiojapjaoijfpaoijsdfpoali 77777a7777 [[email protected] grep]# grep -v '^#' inittab | grep -v '^&'##不以#开头的行中,同时不以&开头的行 999999999 aaaaaa jiojapjaoijfpaoijsdfpoali 77777a7777 [[email protected] grep]# grep '[^0-9]' inittab ##[^0-9]只要有不包含数字的行 # inittab is no longer used when using systemd. # # ADDING CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM. # # Ctrl-Alt-Delete is handled by /usr/lib/systemd/system/ctrl-alt-del.target &&*&*[]%% # # systemd uses 'targets' instead of runlevels. By default, there are two main targets: # # multi-user.target: analogous to runlevel 3 # graphical.target: analogous to runlevel 5 # aaaaaa # To view current default target, run: # systemctl get-default jiojapjaoijfpaoijsdfpoali # # To set a default target, run: # systemctl set-default TARGET.target 77777a7777 # [[email protected] grep]# grep '^[^0-9]' inittab ##以非数字开头的行 # inittab is no longer used when using systemd. # # ADDING CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM. # # Ctrl-Alt-Delete is handled by /usr/lib/systemd/system/ctrl-alt-del.target &&*&*[]%% # # systemd uses 'targets' instead of runlevels. By default, there are two main targets: # # multi-user.target: analogous to runlevel 3 # graphical.target: analogous to runlevel 5 # aaaaaa # To view current default target, run: # systemctl get-default jiojapjaoijfpaoijsdfpoali # # To set a default target, run: # systemctl set-default TARGET.target # [[email protected] grep]# grep -v '^[^0-9]' inittab ##以数字开头的行 999999999 77777a7777
9.3 grep(下)
. 一个任意字符
* *前面的字符重复0个或多次(*要和前面的字符组合起来用)
.* 通配,无论有没有字符都匹配
{} 表示{}前面的字符重复次数范围 ,{2}前面字符重复2次
+ +前面的字符重复1次或多次
? ?前面的字符重复0次或1次
| |表示或者
[[email protected] grep]# grep 'r.o' passwd ## . 匹配任何单个字符 root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin chrony:x:998:996::/var/lib/chroooony:/sbin/nologin [[email protected] grep]# grep 'o*o' passwd ## **左边的字符重复0-n次 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 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown mail:x:8:12:mail:/var/spool/mail:/sbin/nologin operator:x:11:0:operator:/root:/sbin/nologin games:x:12:100:games:/usr/games:/sbin/nologin ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin nobody:x:99:99:Nobody:/:/sbin/nologin systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin dbus:x:81:81:System message bus:/:/sbin/nologin polkitd:x:999:997:User for polkitd:/:/sbin/nologin postfix:x:89:89::/var/spooooool/postfix:/sbin/nologin sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin chrony:x:998:996::/var/lib/chroooony:/sbin/nologin [[email protected] grep]# grep '.*' passwd ##任意一个单字符重复0-n次,就是任意字符 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 games:x:12:100:games:/usr/games:/sbin/nologin ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin nobody:x:99:99:Nobody:/:/sbin/nologin systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin dbus:x:81:81:System message bus:/:/sbin/nologin polkitd:x:999:997:User for polkitd:/:/sbin/nologin postfix:x:89:89::/var/spooooool/postfix:/sbin/nologin sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin chrony:x:998:996::/var/lib/chroooony:/sbin/nologin [[email protected] grep]# grep 'o\{2\}' passwd ##两个o 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/spooooool/postfix:/sbin/nologin chrony:x:998:996::/var/lib/chroooony:/sbin/nologin [[email protected] grep]# grep 'o\{0,3\}' passwd ##o重复0-3次 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 games:x:12:100:games:/usr/games:/sbin/nologin ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin nobody:x:99:99:Nobody:/:/sbin/nologin systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin dbus:x:81:81:System message bus:/:/sbin/nologin polkitd:x:999:997:User for polkitd:/:/sbin/nologin postfix:x:89:89::/var/spooooool/postfix:/sbin/nologin sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin chrony:x:998:996::/var/lib/chroooony:/sbin/nologin
·egrep = grep -E
如果不想用脱义符,那么用egrep或grep -E
[[email protected] grep]# egrep '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/spooooool/postfix:/sbin/nologin chrony:x:998:996::/var/lib/chroooony:/sbin/nologin [[email protected] grep]# grep -E '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/spooooool/postfix:/sbin/nologin chrony:x:998:996::/var/lib/chroooony:/sbin/nologin [[email protected] grep]# egrep '(oo){2}' passwd ## oo字符重复两次 postfix:x:89:89::/var/spooooool/postfix:/sbin/nologin chrony:x:998:996::/var/lib/chroooony:/sbin/nologin [[email protected] grep]# egrep 'o+t' passwd ## +前面的o出现一次或多次 root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin [[email protected] grep]# egrep 'o?t' passwd ## ?前面的o出现0或1次 root:x:0:0:root:/root:/bin/bash shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown halt:x:7:0:halt:/sbin:/sbin/halt operator:x:11:0:operator:/root:/sbin/nologin ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin dbus:x:81:81:System message bus:/:/sbin/nologin polkitd:x:999:997:User for polkitd:/:/sbin/nologin postfix:x:89:89::/var/spooooool/postfix:/sbin/nologin sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin [[email protected] grep]# egrep 'root|nologin' passwd ## | 表示或者,| 前后只要出现一个 root:x:0:0:root:/root:/bin/bash 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 mail:x:8:12:mail:/var/spool/mail:/sbin/nologin operator:x:11:0:operator:/root:/sbin/nologin games:x:12:100:games:/usr/games:/sbin/nologin ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin nobody:x:99:99:Nobody:/:/sbin/nologin systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin dbus:x:81:81:System message bus:/:/sbin/nologin polkitd:x:999:997:User for polkitd:/:/sbin/nologin postfix:x:89:89::/var/spooooool/postfix:/sbin/nologin sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin chrony:x:998:996::/var/lib/chroooony:/sbin/nologin
以上是关于2018-1-15 6周1次课 grep的主要内容,如果未能解决你的问题,请参考以下文章