免费注册 查看新帖 |

Chinaunix

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

[文本处理] [求助]grep \W使用疑问 [复制链接]

论坛徽章:
7
天秤座
日期:2014-08-07 13:56:30丑牛
日期:2014-08-27 20:34:21双鱼座
日期:2014-08-27 22:02:21天秤座
日期:2014-08-30 10:39:11双鱼座
日期:2014-09-21 20:07:532015年亚洲杯之日本
日期:2015-02-06 14:00:282015亚冠之大阪钢巴
日期:2015-11-02 14:50:19
11 [报告]
发表于 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:

论坛徽章:
3
天蝎座
日期:2013-12-10 19:37:11酉鸡
日期:2014-08-04 20:05:38双鱼座
日期:2014-08-09 21:53:54
12 [报告]
发表于 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:

复制代码

论坛徽章:
0
13 [报告]
发表于 2014-08-14 22:28 |只看该作者
回复 12# 欧阳西风


    非常感谢,我学习一下边界词怎么用

论坛徽章:
0
14 [报告]
发表于 2014-08-14 22:47 |只看该作者
回复 12# 欧阳西风


    非常感谢你的解答,\b很好用,我也又学了一招
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP