Chinaunix

标题: [求助]grep \W使用疑问 [打印本页]

作者: chinasea100    时间: 2014-08-14 14:29
标题: [求助]grep \W使用疑问
cat 111.txt
aerrora
<Error>
[ERROR]
:ERROR:

命令:grep -i "\Werror\W" 111.txt
想要得到的结果:
<Error>
[ERROR]
:ERROR:

当前得到的结果:
aerrora

\W 是非单词字符 \W [^A-Za-z0-9],不知道这个命令为什么不行
作者: chinasea100    时间: 2014-08-14 14:31
grep (GNU grep) 2.5.1  这个版本应该是支持的,是不是我哪写的有问题?
作者: yestreenstars    时间: 2014-08-14 14:33
本帖最后由 yestreenstars 于 2014-08-14 14:34 编辑
  1. [root@localhost ~]# cat i
  2. aerrora
  3. <Error>
  4. [ERROR]
  5. :ERROR:
  6. [root@localhost ~]# grep -i '\Werror\W' i
  7. <Error>
  8. [ERROR]
  9. :ERROR:
  10. [root@localhost ~]# grep --version
  11. GNU grep 2.6.3

  12. Copyright (C) 2009 Free Software Foundation, Inc.
  13. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
  14. This is free software: you are free to change and redistribute it.
  15. There is NO WARRANTY, to the extent permitted by law.

  16. [root@localhost ~]#
复制代码

作者: jason680    时间: 2014-08-14 14:34
回复 1# chinasea100

$ grep -iP "\Werror\W" 111.txt
<Error>
[ERROR]
:ERROR:

   
$ man grep
NAME
       grep, egrep, fgrep, rgrep - print lines matching a pattern
       ....

       -P, --perl-regexp
              Interpret  PATTERN  as  a  Perl  regular  expression  (PCRE, see
              below).  This is highly experimental and grep  -P  may  warn  of
              unimplemented features.



作者: chinasea100    时间: 2014-08-14 14:40
回复 4# jason680


    奇怪,我得到的结果与你的不同

grep -iP "\Werror\W" 111.txt
aerrora
作者: chinasea100    时间: 2014-08-14 14:42
回复 3# yestreenstars


    那是我grep版本的问题?
看网上说的\W对grep 2.5.1是支持的

作者: chinasea100    时间: 2014-08-14 14:54
回复 3# yestreenstars


    发现一个现象,如果不用-i来忽略大小写,\W是可用的

$ grep '\WError\W' 111.txt
<Error>
作者: yestreenstars    时间: 2014-08-14 14:54
回复 7# chinasea100

你通过info grep看看里面有没有\W
   
作者: chinasea100    时间: 2014-08-14 14:55
回复 4# jason680


    用下面的笨方法请解决问题:

$ grep '\W[Ee][Rr][Rr][Oo][Rr]\W' 111.txt
<Error>
[ERROR]
:ERROR:
error
作者: yestreenstars    时间: 2014-08-14 14:55
回复 7# chinasea100

那你man grep看一下支持-i参数不
   
作者: MeRcy_PM    时间: 2014-08-14 17:27
       -i, --ignore-case
              Ignore case distinctions in  both  the  PATTERN  and  the  input
              files.  (-i is specified by POSIX.)
我这有-i,但是貌似把整个正则表达式都忽略大小写了,所以变成了\w吧?

hcc-30:~/mytest/shell> cat bbb
aerrora
<Error>
[ERROR]
:ERROR:


hcc-30:~/mytest/shell> grep -Pi '\Werror\W' bbb
aerrora
herongguang@hcc-30:~/mytest/shell> grep --version
GNU grep 2.5.2

Copyright (C) 1988, 1992-2002, 2004, 2005  Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

hcc-30:~/mytest/shell>

hcc-30:~/mytest/shell> grep -Pi '[^a-z]error[^a-z]' bbb<Error>
[ERROR]
:ERROR:
作者: 欧阳西风    时间: 2014-08-14 19:52
本帖最后由 欧阳西风 于 2014-08-14 19:53 编辑

回复 1# chinasea100


    我在cygwin中测试输出跟你的不太一样

  1. $ grep -i "\Werror\W" a.txt
  2. <Error>
  3. [ERROR]
  4. :ERROR:

复制代码
我的grep版本:

  1. $ grep -V
  2. grep (GNU grep) 2.16
  3. Copyright (C) 2014 Free Software Foundation, Inc.
  4. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
  5. This is free software: you are free to change and redistribute it.
  6. There is NO WARRANTY, to the extent permitted by law.

  7. Written by Mike Haertel and others, see <http://git.sv.gnu.org/cgit/grep.git/tree/AUTHORS>.

复制代码
最后,lz这个问题,我觉着可以不用\W来解决的吧,grep默认是支持单词边界的,可以直接使用单词边界:

  1. $ grep -i '\berror\b' a.txt
  2. <Error>
  3. [ERROR]
  4. :ERROR:

  5. $ grep -i '\<error\>' a.txt
  6. <Error>
  7. [ERROR]
  8. :ERROR:

复制代码

作者: chinasea100    时间: 2014-08-14 22:28
回复 12# 欧阳西风


    非常感谢,我学习一下边界词怎么用
作者: chinasea100    时间: 2014-08-14 22:47
回复 12# 欧阳西风


    非常感谢你的解答,\b很好用,我也又学了一招




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