Chinaunix

标题: awk扩展正则为什么不好使了呢 [打印本页]

作者: zhaobin81    时间: 2012-02-15 14:10
标题: awk扩展正则为什么不好使了呢
[root@db1 a]# cat file
usa2-error.log.111.bz2
sdfsdf111sdfs
[root@db1 a]# awk '/111/' file
usa2-error.log.111.bz2
sdfsdf111sdfs
[root@db1 a]# awk '/111{3}/' file
[root@db1 a]#
作者: yinyuemi    时间: 2012-02-15 14:15
回复 1# zhaobin81


    是不是应该这样:

awk '/1{3}/' file
作者: zhaobin81    时间: 2012-02-15 14:16
回复 2# yinyuemi


    这样也不行试过了
作者: zhaobin81    时间: 2012-02-15 14:20
回复 1# zhaobin81


   
[root@db1 a]# ls | awk  '/[0-9][0-9]+/{a=$0;print $a;sub(/[0-9][0-9]+/,"tttttttt",$0);print $a}'
usa2-error.log.1111.bz2
usa2-error.log.tttttttt.bz2
usa2-error.log.20120211.bz2
usa2-error.log.tttttttt.bz2

修改了$0 a也会变

这个也不太明白
难道付的是引用吗
作者: yinyuemi    时间: 2012-02-15 14:23
回复 4# zhaobin81


    使用awk的变量不要dollar,

ls | awk  '/[0-9][0-9]+/{a=$0;print a;sub(/[0-9][0-9]+/,"tttttttt",$0);print a}'

另外,

echo 111 |awk '/1{3}/'
111


作者: zhaobin81    时间: 2012-02-15 14:27
回复 5# yinyuemi


    [root@db1 a]# cat file
usa2-error.log.111.bz2
sdfsdf111sdfs
[root@db1 a]# awk '/1{3}/'

[root@db1 a]# awk '/1{3}/' file

为什么这样配置不上呢
作者: zhaobin81    时间: 2012-02-15 14:29
回复 5# yinyuemi


    [root@db1 a]# echo 111 |awk '/1{3}/'
[root@db1 a]# echo 111 |awk '/1{3}/'
[root@db1 a]# echo 111 |awk '/1{3}/'
[root@db1 a]#


我这不行啊
作者: yinyuemi    时间: 2012-02-15 14:30
本帖最后由 yinyuemi 于 2012-02-15 14:31 编辑

回复 7# zhaobin81


    看下你的版本,我的是
awk --v
GNU Awk 4.0.0
Copyright (C) 1989, 1991-2011 Free Software Foundation


   或使用 --re-interval  选项
作者: zhaobin81    时间: 2012-02-15 14:35
回复 8# yinyuemi


    [root@db1 a]# awk --v
GNU Awk 3.1.5
[root@db1 a]#
[root@db1 a]# echo 111 |awk --re-interval   '/1{3}/'
111
这个参数怎么解释呢?
作者: yinyuemi    时间: 2012-02-15 14:36
回复 9# zhaobin81


   
--re-interval
              Enable the use of interval expressions in regular expression matching (see  Regular  Expressions,
              below).   Interval  expressions  were not traditionally available in the AWK language.  The POSIX
              standard added them, to make awk and egrep consistent with each other.   However,  their  use  is
              likely  to  break  old  AWK  programs, so gawk only provides them if they are requested with this
              option, or when --posix is specified.

作者: ywlscpl    时间: 2012-02-15 14:42
试试
awk --re-interval '/1{3}/'
作者: zhaobin81    时间: 2012-02-15 14:44
回复 10# yinyuemi


    能用中文解释一下吗?
作者: yinyuemi    时间: 2012-02-15 14:48
回复 12# zhaobin81


    简单的讲,如果使用awk的区间表达式(interval expressions),如 r{m,n},需要使用--re-interval这个选项

   你可以google下 正则的区间表达式
作者: zhaobin81    时间: 2012-02-15 14:52
回复 13# yinyuemi


    明白了
多谢
作者: zooyo    时间: 2012-02-15 19:56
提示: 作者被禁止或删除 内容自动屏蔽




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