Chinaunix首页 | 论坛 | 博客
  • 博客访问: 203143
  • 博文数量: 73
  • 博客积分: 2010
  • 博客等级: 大尉
  • 技术积分: 750
  • 用 户 组: 普通用户
  • 注册时间: 2008-03-13 18:32
文章分类

全部博文(73)

文章存档

2009年(1)

2008年(72)

我的朋友

分类: LINUX

2008-03-13 19:35:58

% vi letter

−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−

Hi tom,

I think I failed my anatomy test yesterday. I had a terrible

stomach ache. I ate too many fried green tomatoes.

Anyway, Tom, I need your help. I'd like to make the test up

tomorrow, but don't know where to begin studying. Do you

think you could help me? After work, about 7 PM, come to

my place and I'll treat you to pizza in return for your help. Thanks.

Your pal,

guy@phantom

1,当模式中存在空格或者引号时必须使用引号。
2,grep的选项:
-b,给出匹配行所在的block。
-c,输出匹配的行数。
-h,Does not display the filenames
-l,只显示存在匹配行所在文件的名称。
-n,在匹配行之前加上行号。
-s,若出现错误则显示该错误信息,否则不显示任何信息。
-v,打印不匹配的行。
-w,将其后的表达式作为一个单词进行匹配。不是所有的grep版本都支持。eg,
  •     vi letter
  •     grep tom letter
  •     grep -w tom letter
比较输出结果的不同。
3,grep执行之后会返回一个结束状态量,成功返回0,否则返回1,这是它与awk,sed的不同之处。eg,

在/etc/passwd查找一个不存在的用户。
grep '      ' /etc/passwd
echo $?(返回的状态量保存在$?变量中)
另外在c shell中还会将该变量保存在status变量中。
4,另外grep可以接受多个文件作为输入。单词。
   

% cat datafile

northwest NW Charles Main 3.0 .98 3 34

western WE Sharon Gray 5.3 .97 5 23

southwest SW Lewis Dalsass 2.7 .8 2 18

southern SO Suan Chin 5.1 .95 4 15

southeast SE Patricia Hemenway 4.0 .7 4 17

eastern EA TB Savage 4.4 .84 5 20

northeast NE AM Main Jr. 5.1 .94 3 13

north NO Margot Weber 4.5 .89 5 9

central CT Ann Stephens 5.7 .94 5 13

#比较一下两行代码的不同:
grep NW datafile
grep NW d*
#The importance of being quoted:
grep TB Savage datafile
grep '
TB Savage' datafile

#?单词,grep对单词的理解,单词可以包含空格。
grep '\<[a-z].*n\>' datafile
#The output is:

northwest NW Charles Main 3.0 .98 3 34
western WE Sharon Gray 5.3 .97 5 23
southern SO Suan Chin 5.1 .95 4 15
eastern EA TB Savage 4.4 .84 5 20
northeast NE AM Main Jr. 5.1 .94 3 1

5,与管道符一起使用

   #打印当前目录下的所有子目录。

  •     ls -l | grep '^d'
  •     cat datafile | grep '\

6,grep命令选项
#打印目录下所有匹配模式的文件名
  grep -l 'NW' *
#打印匹配的行数
  grep -c '^northwest' datafile
#打印不匹配的航
  grep -v 'TB Savage' datafile



   





阅读(611) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~