|
12、grep及正则表达式
2021年07月13日 |
|
12.1 grep egrep:扩展正则表达式 fgrep(快速的搜索): 不支持正则表达式,直接搜索字符本身 grep: 根据模式搜索文本,并将符合模式的文本行显示出来。 Pattern(模式): 文本字符和正则表达式的元字符组合而成的匹配条件 grep [options] PATTERN [FILE…] -i:忽略字母大小写 –color:对于被选中的目标用颜色显示 -v: 显示没有被模式匹配到的行 -o:只显示被模式匹配到的字符串 12.2 正则表达式(REGular EXPerssion,REGEXP) 通常使用的匹配的字符 *: 匹配任意长度的任意字符 ?: 任意单个字符 []:指定范围内的内容 [^]:制定范围外的内容 12.2.1 正则表达式的元字符: 1、元字符 . : 匹配任意单个字符 []: 匹配指定范围内的任意单个字符 [^]:匹配指定范围外的任意单个字符 字符集合:[:digit:], [:lower:], [:upper:], [:punct:], [:space:], [:alpha:], [:alnum:] [root@Daniel-R480 ~]# grep ‘r..t’ /etc/passwd root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin [root@Daniel-R480 ~]# 2、匹配次数(贪婪模式,尽可能多的匹配): *: 匹配其前面的字符任意次 a, b, ab, aab, acb, adb, amnb a*b:a出现任意次之后跟着b [root@Daniel-R480 ~]# grep ‘a*b’ test.txt b ab asb aab abb ammjdjfdjb asdbsdabsfdab a?b:匹配a任意次,然后加上b [root@Daniel-R480 ~]# grep ‘a\?b’ test.txt b ab asb aab abb ammjdjfdjb asdbsdabsfdab a.*b:a开头,b结尾,中间跟任意字符 [root@Daniel-R480 ~]# grep ‘a.*b’ test.txt ab asb aab abb ammjdjfdjb asdbsdabsfdab 3、匹配规则 .*: 任意长度的任意字符 \?: 匹配其前面的字符1次或0次 \{m,n\}:匹配其前面的字符至少m次,至多n次 \{1,\} \{0,3\} [root@Daniel-R480 ~]# grep ‘a\{1,3\}b’ test.txt ab aab abb asdbsdabsfdab [root@Daniel-R480 ~]# grep ‘a.\{1,3\}b’ test.txt asb aab abb asdbsdabsfdab [root@Daniel-R480 ~]# 4、位置锚定: ^: 锚定行首,此字符后面的任意内容必须出现在行首 $: 锚定行尾,此字符前面的任意内容必须出现在行尾 ^$: 空白行 \<或\b: 锚定词首,其后面的任意字符必须作为单词首部出现 \>或\b: 锚定词尾,其前面的任意字符必须作为单词的尾部出现 [root@Daniel-R480 ~]# grep ‘r..t’ /etc/passwd root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin [root@Daniel-R480 ~]# grep ‘&r..t’ /etc/passwd [root@Daniel-R480 ~]# grep ‘^r..t’ /etc/passwd root:x:0:0:root:/root:/bin/bash [root@Daniel-R480 tmp]# grep “root\>” ./test.txt This is root the user is mroot chroot is a command [root@Daniel-R480 tmp]# grep “\>root” ./test.txt [root@Daniel-R480 tmp]# grep “root\>” ./test.txt This is root the user is mroot chroot is a command [root@Daniel-R480 tmp]# grep “\<root” ./test.txt This is root rooter is a dog’s name [root@Daniel-R480 tmp]# grep “\<root\>” ./test.txt This is root [root@Daniel-R480 tmp]# 5、分组: \(\):把内容分组 \(ab\)*:ab可以出现任意次 [root@Daniel-R480 ~]# cat test.txt a b ab asb aab abb ammjdjfdjb asdbsdabsfdab [root@Daniel-R480 ~]# grep “\(ab\)*” test.txt a b ab asb aab abb ammjdjfdjb asdbsdabsfdab [root@Daniel-R480 ~]# grep “\(ab\)\?” test.txt a b ab asb aab abb ammjdjfdjb asdbsdabsfdab [root@Daniel-R480 ~]# 6、后向引用(被前面括号括起来的字符,可以在后面继续引用他) \1: 引用第一个左括号以及与之对应的右括号所包括的所有内容 \2:引用第二个左括号以及与之对应的右括号所包括的所有内容 \3:引用第三个左括号以及与之对应的右括号所包括的所有内容 He love his lover. She like her liker. He like his lover. [root@Daniel-R480 ~]# vim test2.txt [root@Daniel-R480 ~]# grep ‘l..e’ test2.txt He love his lover. She like her liker. He like his lover. She love her liker. [root@Daniel-R480 ~]# grep ‘l..e.*l..e’ test2.txt He love his lover. She like her liker. He like his lover. She love her liker. [root@Daniel-R480 ~]# vim test2.txt [root@Daniel-R480 ~]# grep ‘l..e.*l..e’ test2.txt He love his lover. She like her liker. He like his lover. She love her liker [root@Daniel-R480 ~]# grep ‘l..e.*\1’ test2.txt grep: Invalid back reference [root@Daniel-R480 ~]# grep ‘\(l..e\).*\1’ test2.txt He love his lover. She like her liker. l..e:l开头,e结尾 练习: 1、显示/proc/meminfo文件中以不区分大小的s开头的行; [root@Daniel-R480 ~]# grep -i ‘^s’ /proc/meminfo SwapCached: 0 kB SwapTotal: 31018612 kB SwapFree: 31018612 kB Shmem: 17720 kB Slab: 13868 kB SReclaimable: 6744 kB SUnreclaim: 7124 kB [root@Daniel-R480 ~]# grep ‘^[sS]’ /proc/meminfo SwapCached: 0 kB SwapTotal: 31018612 kB SwapFree: 31018612 kB Shmem: 17720 kB Slab: 13868 kB SReclaimable: 6744 kB SUnreclaim: 7124 kB [root@Daniel-R480 ~]# 2、显示/etc/passwd中以nologin结尾的行; [root@Daniel-R480 ~]# grep ‘nologin$’ /etc/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:998:User for polkitd:/:/sbin/nologin rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin postfix:x:89:89::/var/spool/postfix:/sbin/nologin chrony:x:998:995::/var/lib/chrony:/sbin/nologin test3:x:10014:10014::/home/test3:/sbin/nologin 取出默认shell为/sbin/nologin的用户列表 [root@Daniel-R480 ~]# grep ‘nologin$’ /etc/passwd | cut -d: -f1 bin daemon adm lp operator games ftp nobody systemd-network dbus polkitd rpc rpcuser nfsnobody sshd postfix chrony test3 [root@Daniel-R480 ~]# 取出默认shell为bash,且其用户ID号最小的用户的用户名 [root@Daniel-R480 ~]# grep ‘bash$’ /etc/passwd | sort -n -t: -k3 | head -1 | cut -d: -f1 root 3、显示/etc/inittab中以#开头,且后面跟一个或多个空白字符,而后又跟了任意非空白字符的行; [root@Daniel-R480 ~]# grep “^#[[:space:]]\{1,\}[^[:space:]]” /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: # multi-user.target: analogous to runlevel 3 # graphical.target: analogous to runlevel 5 # To view current default target, run: # systemctl get-default # To set a default target, run: # systemctl set-default TARGET.target [root@Daniel-R480 ~]# 4、显示/boot/grub/grub.conf文件中以一个或多个空白字符开头的行; [root@Daniel-R480 ~]# grep ‘^[[:space:]]\{1,\}’ /boot/grub/grub.conf root (hd0) kernel /boot/vmlinuz-3.10.0-957.1.3.el7.x86_64 ro root=UUID=f41e390f-835b-4223-a9bb-9b45984ddf8d console=hvc0 LANG=en_US.UTF-8 initrd /boot/initramfs-3.10.0-957.1.3.el7.x86_64.img |