学习grep 遇到的怪事

sh/bash/dash/ksh/zsh等Shell脚本
回复
wutong
帖子: 345
注册时间: 2007-10-22 16:54

学习grep 遇到的怪事

#1

帖子 wutong » 2011-02-27 10:46

学习《鳥哥的私房菜》中的内容,学到命令 grep 时,遇到怪事如下:

先编辑oo.txt
"Open Source" is a good mechanism to develop programs.
apple is my favorite food.
Football game is not use feet only.
The soup taste good.
I cant't finish the test.
google is the best tools for serch keyword.
goooooogle yes!

$ grep -n '[^fgot]oo' oo.txt
3:Football game is not use feet only.

$ grep -n '[^a-z]oo' oo.txt
应该也给出上述命令的标准输出。但是我的屏幕没有任何输出。这是怎么回事?
wutong
帖子: 345
注册时间: 2007-10-22 16:54

Re: 学习grep 遇到的怪事

#2

帖子 wutong » 2011-02-27 10:58

再试如下:
yuanbx@hp:~$ grep -n '[^g-z]oo' oo.txt
2:apple is my favorite food.
3:Football game is not use feet only.
yuanbx@hp:~$ grep -n '[^f-z]oo' oo.txt
yuanbx@hp
wutong
帖子: 345
注册时间: 2007-10-22 16:54

Re: 学习grep 遇到的怪事

#3

帖子 wutong » 2011-02-28 9:36

今天我用以前的光盘启动了一下电脑,使用了一下ubuntu9.10, grep 的功能很正常。不知是ubuntu10.10 的毛病,还是
grep 2.6.3 的臭虫。ubuntu 9.10下的grep 是2.5.10.
wutong
帖子: 345
注册时间: 2007-10-22 16:54

Re: 学习grep 遇到的怪事

#4

帖子 wutong » 2011-02-28 11:10

我下载装上了grep 2.7, 试後没有了上述问题。如果你的系统也是ubuntu10.10. 试一试与我见到的情况是不是一样。
头像
eexpress
帖子: 58428
注册时间: 2005-08-14 21:55
来自: 长沙

Re: 学习grep 遇到的怪事

#5

帖子 eexpress » 2011-02-28 11:20

过时的书,要摘录了看。别抄。
● 鸣学
MaskRay
帖子: 61
注册时间: 2010-04-29 22:00
系统: Gentoo Linux ~amd64
联系:

Re: 学习grep 遇到的怪事

#6

帖子 MaskRay » 2011-02-28 11:25

$ echo $GREP_OPTIONS

看是否启用了 -i -y --ignore-case 三者之一
头像
sunsol
帖子: 55
注册时间: 2009-01-04 21:55

Re: 学习grep 遇到的怪事

#7

帖子 sunsol » 2011-02-28 16:41

gnu grep对[a-z]有的时候会有问题,特别是中文环境
主要是因为其考虑到非英文字符
跟环境设置有关,把LANG设成en_US试试看
windows下记得好像是把LC_ALL设成C
only_one
帖子: 186
注册时间: 2010-05-25 16:49

Re: 学习grep 遇到的怪事

#8

帖子 only_one » 2011-03-05 21:08

sunsol 写了:gnu grep对[a-z]有的时候会有问题,特别是中文环境
主要是因为其考虑到非英文字符
跟环境设置有关,把LANG设成en_US试试看
windows下记得好像是把LC_ALL设成C
楼上正解
设成 LANG=C
或者使用[^[:lower:]]代替[^a-z]
上次由 only_one 在 2011-03-05 21:11,总共编辑 1 次。
only_one
帖子: 186
注册时间: 2010-05-25 16:49

Re: 学习grep 遇到的怪事

#9

帖子 only_one » 2011-03-05 21:10

这个问题鸟哥有说过哦
回复