Chinaunix

标题: grep '[Ff]irst' *.txt 和 grep [Ff]irst *.txt 有无区别? [打印本页]

作者: zcsgamer    时间: 2009-12-17 20:35
标题: grep '[Ff]irst' *.txt 和 grep [Ff]irst *.txt 有无区别?
grep '[Ff]irst' *.txt 和 grep [Ff]irst *.txt 有无区别?
作者: blackold    时间: 2009-12-17 21:18
标题: 回复 #1 zcsgamer 的帖子
有区别。

结果是否有区别取决执行环境。
作者: wqfhenanxc    时间: 2009-12-17 21:34
一样,
首先,当'[Ff]irst' 由shell传递给grep作正则表达式时,单引号已经被shell去掉了,作grep参数的正则表达式还是[Ff]irst.
其次,单引号在shell中是特殊字符,但它在正则表达式中并无特别之处。
在shell中执行命令grep \'[Ff]irst\'会匹配到类似 haha'first'hahha 这样的行。
不知道楼主有什么困惑?
作者: zcsgamer    时间: 2009-12-17 21:43
标题: 回复 #2 blackold 的帖子
黑哥能不能举个具体的例子?最好包括结果。
作者: zcsgamer    时间: 2009-12-17 21:43
标题: 回复 #3 wqfhenanxc 的帖子
ABS上讲有区别,我不知道是什么区别,这就是困惑了。
作者: wqfhenanxc    时间: 2009-12-17 21:47
标题: 回复 #5 zcsgamer 的帖子
哦,ABS都讲有区别了,那咱们期待解答了。。
作者: zcsgamer    时间: 2009-12-17 21:51
标题: 回复 #6 wqfhenanxc 的帖子
ABS的解答我没太看明白,还是期待黑哥的解答吧。
作者: nhw_cs    时间: 2009-12-17 22:18
原帖由 zcsgamer 于 2009-12-17 20:35 发表
grep '[Ff]irst' *.txt 和 grep [Ff]irst *.txt 有无区别?



也是好问题! 理解文件通配符工作原理的经典例子. 从写法上看, 答案是有区别! 实际执行时有无区别取决于当前目录下有哪些文件. 你找个环境测试一下, 早当前目录下建立一个名字为first1.txt的文件, 然后执行这两个命令看看结果 .... 删除first1.txt再次执行 ...

自己先体会下吧, 等我兴趣打更多的字时再解释 ....
作者: zcsgamer    时间: 2009-12-17 22:25
标题: 回复 #8 nhw_cs 的帖子
好的,我自己试试看,太感谢了。
作者: zcsgamer    时间: 2009-12-17 22:32
好像没什么区别,结果都一样,还是偷懒等指点吧。
作者: nhw_cs    时间: 2009-12-17 22:37
原帖由 zcsgamer 于 2009-12-17 22:25 发表
好的,我自己试试看,太感谢了。


其实你这孩子挺可教的, 看注册时间应该是初学者, 此时就能刨根问题关注这样的问题,相信不久就能成为高手. 当你理解了UNIX很多现象背后的原理, 必然豁然开朗 .... 很多看似理所当然的事情, 背后都有更多的秘密等待挖掘,  学无止境啊
作者: nhw_cs    时间: 2009-12-17 22:43
原帖由 zcsgamer 于 2009-12-17 22:25 发表
好的,我自己试试看,太感谢了。


我看错了 ..你举的例子好像有问题.. 应该是 grep '[Ff]irst *’.txt  与 grep [Ff]irst *.txt 比较, 这是有区别的
作者: zcsgamer    时间: 2009-12-17 22:44
原帖由 nhw_cs 于 2009-12-17 22:37 发表


其实你这孩子挺可教的, 看注册时间应该是初学者, 此时就能刨根问题关注这样的问题,相信不久就能成为高手. 当你理解了UNIX很多现象背后的原理, 必然豁然开朗 .... 很多看似理所当然的事情, 背后都有更多的秘 ...

呵呵,感谢前辈指点。
作者: zcsgamer    时间: 2009-12-17 22:50
原帖由 nhw_cs 于 2009-12-17 22:43 发表


我看错了 ..你举的例子好像有问题.. 应该是 grep '[Ff]irst *’.txt  与 grep [Ff]irst *.txt 比较, 这是有区别的

我有看了看,ABS上就是那样写的,先是给出这个命令:grep '[Ff]irst' *.txt

然后:『注意一下未引用的 grep [Ff]irst *.txt 在Bash shell下的行为.』
针对上一句,还有一个注释:『除非正好当前工作目录下有一个名字为 first的文件. 然而这是引用的另一个原因. (感谢, Harald Koenig, 指出这一点. 』

我再研究一下你说的那两者有什么区别。
作者: zcsgamer    时间: 2009-12-17 22:51
不早了,我先下了。
作者: nhw_cs    时间: 2009-12-17 22:58
原帖由 zcsgamer 于 2009-12-17 22:51 发表
不早了,我先下了。


书上说的没错, 你最初的两个写法是有区别的, 我说的两个写法也是有区别的, 但解释的原理是相似的 .... 理解这类问题的关键是'文件通配符'到底是如何工作的 .... 有点太晚了, 脑子有点晕 ..你那个\\z的问题我现在也有些困惑, 明天再研究
作者: blackold    时间: 2009-12-17 23:03
原帖由 wqfhenanxc 于 2009-12-17 21:47 发表
哦,ABS都讲有区别了,那咱们期待解答了。。

尽信书不如无书!
作者: waker    时间: 2009-12-18 08:34
标题: 回复 #15 zcsgamer 的帖子
给你个建议
如果看不懂Advanced Bash Scripting Guide
不妨先看Bash Guide for Beginners
作者: beginner-bj    时间: 2009-12-18 13:02
应该是生成一个叫first的文件,然后删除,两种情况做对比。
生成一个叫First的文件也行。
作者: yazi0127    时间: 2009-12-18 13:17
找了个环境试了一下:
cat aa.txt
    first
    second
    First
    Second
grep [Ff]irst *.txt
    无返回
grep '[Ff]irst' *.txt
    aa.txt:first
    aa.txt:First
grep "[Ff]irst" *.txt
    aa.txt:first
    aa.txt:First

和环境/操作系统有关?期待高手解答
作者: zcsgamer    时间: 2009-12-18 13:59
原帖由 yazi0127 于 2009-12-18 13:17 发表
找了个环境试了一下:
cat aa.txt
    first
    second
    First
    Second
grep [Ff]irst *.txt
    无返回
grep '[Ff]irst' *.txt
    aa.txt:first
    aa.txt:First
grep "[Ff]irst" *.txt
...

我这里为什么没有任何区别?
作者: ywlscpl    时间: 2009-12-18 14:07
  1. [root@Mylinux tmp]# cat a.txt
  2. a.txt first
  3. a.txt First
  4. [root@Mylinux tmp]# cat first
  5. first
  6. First
  7. [root@Mylinux tmp]# grep '[fF]irst' *.txt
  8. a.txt first
  9. a.txt First
  10. [root@Mylinux tmp]# grep [fF]irst *.txt  
  11. a.txt first
  12. [root@Mylinux tmp]# rm first
  13. rm:是否删除 一般文件 “first”? y
  14. [root@Mylinux tmp]# grep '[fF]irst' *.txt
  15. a.txt first
  16. a.txt First
  17. [root@Mylinux tmp]# grep [fF]irst *.txt  
  18. a.txt first
  19. a.txt First
  20. [root@Mylinux tmp]#
复制代码


如果命令是grep [fF]irst *.txt,当存在first文件时,经过Pathname Expansion后,最后的命令是grep first a.txt(如果只有一个.txt文件)
当不存在first文件时,经过Pathname Expansion后,最后的命令是grep [fF]irst a.txt(如果只有一个.txt文件)

Pathname Expansion
       After  word  splitting,  unless  the -f option has been set, bash scans
       each word for the characters *, ?, and [.  If one of  these  characters
       appears,  then  the word is regarded as a pattern, and replaced with an
       alphabetically sorted list of file names matching the pattern.   If  no
       matching  file  names  are found, and the shell option nullglob is dis-
       abled, the word is left unchanged.
  If the nullglob option is set,  and
       no  matches  are  found,  the  word  is removed.  If the failglob shell
       option is set, and no matches are found, an error  message  is  printed
       and  the  command  is  not executed.  If the shell option nocaseglob is
       enabled, the match is performed without regard to the  case  of  alpha-
       betic  characters.   When a pattern is used for pathname expansion, the
       character ‘‘.’’  at the start of a  name  or  immediately  following  a
       slash  must  be  matched explicitly, unless the shell option dotglob is
       set.  When matching a pathname, the  slash  character  must  always  be
       matched  explicitly.   In  other  cases,  the  ‘‘.’’   character is not
       treated specially.

[ 本帖最后由 ywlscpl 于 2009-12-18 14:16 编辑 ]
作者: yazi0127    时间: 2009-12-18 16:16
原帖由 zcsgamer 于 2009-12-18 13:59 发表

我这里为什么没有任何区别?



是SHELL的问题,我切到bash就没有区别了。之前的那个try是在C Shell下。
作者: wqfhenanxc    时间: 2009-12-18 17:00
标题: 回复 #22 ywlscpl 的帖子
嗯,多谢,基本了解了。
但还有个问题:pathname expansion在什么情况下才发生? 我看资料上说的是发生在shell对输入的word split之后,难道我输入的任何含有glob word的字串,不管我是不是用来匹配文件路径的,都会被pathname expansion?
作者: blackold    时间: 2009-12-18 17:20
标题: 回复 #24 wqfhenanxc 的帖子
基本上如此。
作者: zcsgamer    时间: 2009-12-18 17:39
原帖由 ywlscpl 于 2009-12-18 14:07 发表
[root@Mylinux tmp]# cat a.txt
a.txt first
a.txt First
[root@Mylinux tmp]# cat first
first
First
[root@Mylinux tmp]# grep '[fF]irst' *.txt
a.txt first
a.txt First
[root@Mylinux tmp]# grep ...

我还是不明白,为什么当存在first文件时,经过Pathname Expansion后,最后的命令会变成grep first a.txt。

水平太差,需要加倍努力啊。

[ 本帖最后由 zcsgamer 于 2009-12-18 17:40 编辑 ]
作者: blackold    时间: 2009-12-18 17:45
标题: 回复 #26 zcsgamer 的帖子
慢慢来吧,不急。
作者: zcsgamer    时间: 2009-12-18 19:23
吃了饭,溜达的一圈,想明白了,是 [fF]irst 这里也给 Pathname Expansion 了,原以为只是 *.txt 这里才会被 Pathname Expansion ,而 [fF]irst  这里只是要匹配的内容,不会被扩展。
作者: zcsgamer    时间: 2009-12-18 19:25
非常感谢nhw_cs和黑哥。
作者: elsove812    时间: 2009-12-22 11:31
[dmma@ldconf shell]$ cat aa.txt
+ cat aa.txt
first
First
firstfgsdfg
dfsgdffirst
safsdFirstdfsgd
dshdf
++ echo -ne '\033]0;dmma@ldconf:~/src/shell'
[dmma@ldconf shell]$ cat first
+ cat first
gsdsdgfs
first
++ echo -ne '\033]0;dmma@ldconf:~/src/shell'
[dmma@ldconf shell]$ grep '[fF]irst' *.txt
+ grep '[fF]irst' aa.txt
first
First
firstfgsdfg
dfsgdffirst
safsdFirstdfsgd
++ echo -ne '\033]0;dmma@ldconf:~/src/shell'
[dmma@ldconf shell]$ grep "[fF]irst" *.txt
+ grep '[fF]irst' aa.txt
first
First
firstfgsdfg
dfsgdffirst
safsdFirstdfsgd
++ echo -ne '\033]0;dmma@ldconf:~/src/shell'
[dmma@ldconf shell]$ grep [fF]irst *.txt
+ grep first aa.txt
first
firstfgsdfg
dfsgdffirst
++ echo -ne '\033]0;dmma@ldconf:~/src/shell'




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2