grep的使用(二)

王朝other·作者佚名  2006-11-24
窄屏简体版  字體: |||超大  

# ls -l | grep "^d"(仅列出当前目录下的所有目录)

drwxr-xr-x 2 root other 512 11月 25 22:41 ldap

#

# ls -l | grep"^[^d]"(仅列出当前目录下的所有文件)

-rw-r--r-- 1 root other 4987 11月 25 23:22 grep.log

-rw-r--r-- 1 root other 0 11月 26 22:34 grep.log2

-rw-r--r-- 1 root other 267 11月 19 19:37 ifconfig

#

# ls -l|grep "^d.....x..x"(仅列出当前目录下符合“d.....x..x”权限的目录)

drwxr-xr-x 2 root other 512 11月 25 22:41 ldap

#

# grep "mailsrv" /etc/passwd(抽出/etc/passwd中“mailsrv”的用户信息)

mailsrv:x:101:3:iplanet mail user:/iplanet/server5/bin/msg/admin/bin:/bin/sh

#

# grep "mailsrv" /etc/passwd >mailuser.log

# (抽出/etc/passwd中“mailsrv”的用户信息输出到一个文件(mailsrv.log)中)

# more mailuser.log

mailsrv:x:101:3:iplanet mail user:/iplanet/server5/bin/msg/admin/bin:/bin/sh

#

# ps -ef|grep telnet(在显示的进程中抽出“telnet”进程)

root 321 165 0 22:29:33 ? 0:00 in.telnetd

root 638 434 0 23:35:38 pts/1 0:00 grep telnet

# ps -ef|grep telnet | grep -v grep (在显示的进程中抽出“telnet”进程;并丢弃ps中的grep进程)

root 321 165 0 22:29:33 ? 0:00 in.telnetd

#

# ps -ef|grep -v telnet(将抽出除“telnet”之外的所有进程)

# cat zhao.conf(再来看一下我们使用的文件内容)

48 Dec 3BC1997 LPSX 68.00 LVX2A 138

483 Sept 5AP1996 USP 65.00 LVX2C 189

47 Oct 3ZL1998 LPSX 43.00 KVM9D 512

219 dec 2CC1999 CAD 23.00 PLV2C 68

484 nov 7PL1996 CAD 49.00 PLV2C 234

487 may 5PA1998 USP 37.00 KVM9D 644

471 May 7Zh1999 UDP 37.00 KV30D 643

# egrep "(3ZL|2CC)" zhao.conf(使用egrep来抽取文件(zhao.conf)中前三个字符匹配“3ZL”或“2CC”的行)

47 Oct 3ZL1998 LPSX 43.00 KVM9D 512

219 dec 2CC1999 CAD 23.00 PLV2C 68

# grep "48" zhao.conf |wc(使用grep抽取文件(zhao.conf)中前两个字符匹配“48”的行,并统计出有几行匹配,有多少个

字,这些字占用多少字节的空间)

4 28 224

在UNIX中字处理是一件很烦琐的事情,但这些字处理工具给我们带来了很大的方便;这里grep、egrep(扩展grep,在一些UNIX中

也存在)仅仅是一种工具。但其能灵活的抽出你需要的东东。它将随着你对UNIX的理解程度,逐步变成你手头不可缺少的一个工具

其在shell编程中同样具有举足轻重的位置。grep、egrep和其他字处理工具的组合使用更为灵活、深澳!谢谢!

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航