免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
123
最近访问板块 发新帖
楼主: zcsgamer
打印 上一主题 下一主题

grep '[Ff]irst' *.txt 和 grep [Ff]irst *.txt 有无区别? [复制链接]

论坛徽章:
0
21 [报告]
发表于 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
...

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

论坛徽章:
0
22 [报告]
发表于 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 编辑 ]

论坛徽章:
0
23 [报告]
发表于 2009-12-18 16:16 |只看该作者
原帖由 zcsgamer 于 2009-12-18 13:59 发表

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



是SHELL的问题,我切到bash就没有区别了。之前的那个try是在C Shell下。

论坛徽章:
0
24 [报告]
发表于 2009-12-18 17:00 |只看该作者

回复 #22 ywlscpl 的帖子

嗯,多谢,基本了解了。
但还有个问题:pathname expansion在什么情况下才发生? 我看资料上说的是发生在shell对输入的word split之后,难道我输入的任何含有glob word的字串,不管我是不是用来匹配文件路径的,都会被pathname expansion?

论坛徽章:
5
2015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:50:282015年亚洲杯之朝鲜
日期:2015-03-13 22:47:33IT运维版块每日发帖之星
日期:2016-01-09 06:20:00IT运维版块每周发帖之星
日期:2016-03-07 16:27:44
25 [报告]
发表于 2009-12-18 17:20 |只看该作者

回复 #24 wqfhenanxc 的帖子

基本上如此。

论坛徽章:
0
26 [报告]
发表于 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 编辑 ]

论坛徽章:
5
2015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:50:282015年亚洲杯之朝鲜
日期:2015-03-13 22:47:33IT运维版块每日发帖之星
日期:2016-01-09 06:20:00IT运维版块每周发帖之星
日期:2016-03-07 16:27:44
27 [报告]
发表于 2009-12-18 17:45 |只看该作者

回复 #26 zcsgamer 的帖子

慢慢来吧,不急。

论坛徽章:
0
28 [报告]
发表于 2009-12-18 19:23 |只看该作者
吃了饭,溜达的一圈,想明白了,是 [fF]irst 这里也给 Pathname Expansion 了,原以为只是 *.txt 这里才会被 Pathname Expansion ,而 [fF]irst  这里只是要匹配的内容,不会被扩展。

论坛徽章:
0
29 [报告]
发表于 2009-12-18 19:25 |只看该作者
非常感谢nhw_cs和黑哥。

论坛徽章:
0
30 [报告]
发表于 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'
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP