rep / egrep
語法: grep [-cinvABC] 'word' filename
-c :打印符合要求的行數(shù)
-i :忽略大小寫
-n :在輸出符合要求的行的同時(shí)連同行號(hào)一起輸出
-v :打印不符合要求的行
-A :后跟一個(gè)數(shù)字(有無空格都可以),例如 A2則表示打印符合要求的行以及下面兩行
-B :后跟一個(gè)數(shù)字,例如 B2 則表示打印符合要求的行以及上面兩行
-C :后跟一個(gè)數(shù)字,例如 C2 則表示打印符合要求的行以及上下各兩行
把包含 ‘halt' 的行以及這行下面的兩行都打印出。
[root@localhost ~]# grep -A2 'halt' /etc/passwdhalt:x:7:0:halt:/sbin:/sbin/haltmail:x:8:12:mail:/var/spool/mail:/sbin/nologinuucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
把包含 ‘halt' 的行以及這行上面的兩行都打印出。
[root@localhost ~]# grep -B2 'halt' /etc/passwdsync:x:5:0:sync:/sbin:/bin/syncshutdown:x:6:0:shutdown:/sbin:/sbin/shutdownhalt:x:7:0:halt:/sbin:/sbin/halt
把包含 ‘halt' 的行以及這行上面和下面的各兩行都打印出。
過濾出帶有某個(gè)關(guān)鍵詞的行并輸出行號(hào)
[root@localhost ~]# grep -n 'root' /etc/passwd1:root:x:0:0:root:/root:/bin/bash11:operator:x:11:0:operator:/root:/sbin/nologin
過濾不帶有某個(gè)關(guān)鍵詞的行,并輸出行號(hào)
[root@localhost ~]# grep -nv 'nologin' /etc/passwd1:root:x:0:0:root:/root:/bin/bash6:sync:x:5:0:sync:/sbin:/bin/sync7:shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown8:halt:x:7:0:halt:/sbin:/sbin/halt26:test:x:511:511::/home/test:/bin/bash27:test1:x:512:511::/home/test1:/bin/bash
過濾出所有包含數(shù)字的行
[root@localhost ~]# grep '[0-9]' /etc/inittab# upstart works, see init(5), init(8), and initctl(8).# 0 - halt (Do NOT set initdefault to this)# 1 - Single user mode# 2 - Multiuser, without NFS (The same as 3, if you do not have networking)# 3 - Full multiuser mode# 4 - unused# 5 - X11# 6 - reboot (Do NOT set initdefault to this)id:3:initdefault:
過濾出所有不包含數(shù)字的行
[root@localhost ~]# grep -v '[0-9]' /etc/inittab# inittab is only used by upstart for the default runlevel.## ADDING OTHER CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM.## System initialization is started by /etc/init/rcS.conf## Individual runlevels are started by /etc/init/rc.conf## Ctrl-Alt-Delete is handled by /etc/init/control-alt-delete.conf## Terminal gettys are handled by /etc/init/tty.conf and /etc/init/serial.conf,# with configuration in /etc/sysconfig/init.## For information on how to write upstart event handlers, or how## Default runlevel. The runlevels used are:#
把所有以 ‘#' 開頭的行去除
[root@localhost ~]# grep -v '^#' /etc/inittabid:3:initdefault:
去除所有空行和以 ‘#' 開頭的行
[root@localhost ~]# grep -v '^#' /etc/crontab |grep -v '^$'SHELL=/bin/bashPATH=/sbin:/bin:/usr/sbin:/usr/binMAILTO=rootHOME=/
新聞熱點(diǎn)
疑難解答
圖片精選