@zhaikun
2015-11-24T07:46:21.000000Z
字数 2707
阅读 1075
命令总结
grep:根据模式搜索文本,并符合模式的文本显示出来
pattern:文本字符和正则表达式组合成匹配
[root@yunweibutest001 ~]# grep 'popt' --color ./install.logInstalling popt-1.10.2.3-27.el5.x86_64Installing popt-1.10.2.3-27.el5.i386
-i 表示忽略大小写
--color 高亮显示匹配字符串
-v 显示没有被匹配的字符串
-o 只显示被动模式匹配到的字符串
[root@yunweibutest001 ~]# grep 'popt' -o ./install.logpoptpopt
*:表示任意长度的任意字符
?:任意单个字符
[]:匹配指定范围内的任意单个字符
[:digit:] [:lower:] [:upper:] [:punct:] [:alpha:] [:alnum:]
数字 小写字母 大写字母 标点符号 所有字母 所有字符和字母
元字符
.:表示任意单个字符
[root@yunweibutest001 ~]# grep 'p..t' ./install.logInstalling popt-1.10.2.3-27.el5.x86_64Installing psutils-1.17-26.1.x86_64Installing iptstate-1.4-2.el5.x86_64Installing popt-1.10.2.3-27.el5.i386Installing kpartx-0.4.7-48.el5.x86_64Installing gphoto2-2.2.0-3.el5.x86_64Installing tmpwatch-2.9.7-1.1.el5.5.x86_64Installing parted-1.8.1-29.el5.x86_64Installing portmap-4.0-65.2.2.1.x86_64Installing pygtk2-2.10.1-12.el5.x86_64Installing pygtk2-libglade-2.10.1-12.el5.x86_64Installing parted-1.8.1-29.el5.i386Installing 1:yum-updatesd-0.9-2.el5.noarchInstalling gnome-python2-applet-2.16.0-3.el5.x86_64Installing 1:gnome-applets-2.16.0.1-19.el5.x86_64
1、*:匹配当前字符的任意次
a,b,ab,aab,acb,adb,amnb
a*b
只能匹配a,b,aab其他都不可以找到
2、.*:组合起来表示任意长度和任意字符
a.*b表示a开头,b结尾的任意长度任意字符的匹配项
3、?:匹配其前面的字符1次或0次
\{m,n\}:匹配起前面的字符,表示最少几次,最多几次\{1,\}\{0,3\}
^:锚定行首 此字符后面的任意内容出现在行首$:锚定行尾 此字符后面的任意内容出现在行后^$: 空白行[root@yunweibutest001 ~]# grep 'w$' /etc/inittabca::ctrlaltdel:/sbin/shutdown -t3 -r now以W结束的行[root@yunweibutest001 ~]# grep 'b..h$' /etc/passwdroot:x:0:0:root:/root:/bin/bashzhaik:x:501:502::/home/zhaik:/bin/bash111:x:60001:60001::/home/111:/bin/bashmysql:x:502:503::/home/mysql:/bin/bashnagios:x:503:504::/home/nagios:/bin/bashapache:x:504:506::/home/apache:/bin/bashftpvirtuser:x:505:507::/home/ftp:/bin/bash^$空白行例子[root@yunweibutest001 ~]# grep '^$' /etc/inittab[root@yunweibutest001 ~]#以数字结尾的行[root@yunweibutest001 ~]# grep '[[:digit:]]$' /etc/inittab# 5 - X11l0:0:wait:/etc/rc.d/rc 0l1:1:wait:/etc/rc.d/rc 1l2:2:wait:/etc/rc.d/rc 2l3:3:wait:/etc/rc.d/rc 3l4:4:wait:/etc/rc.d/rc 4l5:5:wait:/etc/rc.d/rc 5l6:6:wait:/etc/rc.d/rc 61:2345:respawn:/sbin/mingetty tty12:2345:respawn:/sbin/mingetty tty23:2345:respawn:/sbin/mingetty tty34:2345:respawn:/sbin/mingetty tty45:2345:respawn:/sbin/mingetty tty56:2345:respawn:/sbin/mingetty tty6# Run xdm in runlevel 5以数字结尾的前面是空格列出来[root@yunweibutest001 ~]# grep '[[:space:]][[:digit:]]$' /etc/inittabl0:0:wait:/etc/rc.d/rc 0l1:1:wait:/etc/rc.d/rc 1l2:2:wait:/etc/rc.d/rc 2l3:3:wait:/etc/rc.d/rc 3l4:4:wait:/etc/rc.d/rc 4l5:5:wait:/etc/rc.d/rc 5l6:6:wait:/etc/rc.d/rc 6# Run xdm in runlevel 5\\<或者\b:其后面的任意字符必须作为单词的首部出现[root@yunweibutest001 ~]# grep "root\>" test1.txtThis is root.The user is mrootchroot is a commandmroot is not a wrod\>或者\b:其前面的任意字符必须作为单词的尾部出现[root@yunweibutest001 ~]# grep "\<root" test1.txtThis is root.rooter is a dog's name
\(\)\(ab\)* 以ab为组