gawk '/^[A-Z][a-z]{3}/{print}‘怎么匹配不到Sjjj

sh/bash/dash/ksh/zsh等Shell脚本
回复
lujinke
帖子: 81
注册时间: 2007-07-20 10:12

gawk '/^[A-Z][a-z]{3}/{print}‘怎么匹配不到Sjjj

#1

帖子 lujinke » 2008-12-04 13:39

如题,gawk '/^[A-Z][a-z]{3}/{print}‘怎么匹配不到Sjjj
grep可以匹配到,怎么gawk匹配不到,gawk里面的正则表达式是可以用x{m}的形式的啊
注:用\{3\}也匹配不到
头像
goodluck1982
帖子: 171
注册时间: 2007-07-05 2:48

Re: gawk '/^[A-Z][a-z]{3}/{print}‘怎么匹配不到Sjjj

#2

帖子 goodluck1982 » 2008-12-06 1:04

这里涉及到gawk的两个问题,如果是第一次遇到都会很迷惑:

1. 在非英文的locale下,默认的英文字母排序方式可能不在是ASCII顺序,即ABCD...XYZabcd...xyz
例如,可能是AaBbCc...Zz或 aAbBcC...zZ,对于中文环境,似乎是后者。那么显而易见[A-Z]也匹配小写字母而[a-z]也匹配大写字母。所以最好用[[:upper:]]和[[:lower:]]来代表大写和小写字母,如果非要用[A-Z]来匹配大写字母的话,可以用设定locale,比如

LANG=C gawk '/A-Z/ {print}' ....

2. 区间操作 {n}, {n,}, {n,m} 等只有在使用 --posix 和 --re-interval 选项时才有效
关于这点,gawk的manpage里提到了

代码: 全选

       r{n}
       r{n,}
       r{n,m}     One or two numbers inside braces denote an interval expression.  If there is one number in the braces, the preceding regular  expression  r  is  repeated  n times.  If there are two numbers separated by a comma, r is repeated n to m times.  If there is one number followed by a comma, then r is repeated at least n times. Interval expressions are only available if either --posix or --re-interval is specified on the command line.
上次由 goodluck1982 在 2008-12-08 10:45,总共编辑 1 次。
lujinke
帖子: 81
注册时间: 2007-07-20 10:12

Re: gawk '/^[A-Z][a-z]{3}/{print}‘怎么匹配不到Sjjj

#3

帖子 lujinke » 2008-12-06 16:53

首先感谢回复,而且答案也应该是正确的,但是用grep和sed在相同的控制台上[A-Z]就只是匹配大写字符
你说的第一点,是特指gawk的实现问题吗

默认的英文排序确实是不是ASCII顺序,是aAbB...zZ
goodluck1982 写了:这里涉及到个问题,如果是第一次遇到都会很迷惑:

1. 在非英文的locale下,默认的英文字母排序方式可能不在是ASCII顺序,即ABCD...XYZabcd...xyz
例如,可能是AaBbCc...Zz或 aAbBcC...zZ,对于中文环境,似乎是后者。那么显而易见[A-Z]也匹配小写字母而[a-z]也匹配大写字母。所以最好用[[:upper:]]和[[:lower:]]来代表大写和小写字母,如果非要用[A-Z]来匹配大写字母的话,可以用设定locale,比如

LANG=C gawk '/A-Z/ {print}' ....

2. 区间操作 {n}, {n,}, {n,m} 等只有在使用 --posix 和 --re-interval 选项时才有效
关于这点,gawk的manpage里提到了

代码: 全选

       r{n}
       r{n,}
       r{n,m}     One or two numbers inside braces denote an interval expression.  If there is one number in the braces, the preceding regular  expression  r  is  repeated  n times.  If there are two numbers separated by a comma, r is repeated n to m times.  If there is one number followed by a comma, then r is repeated at least n times. Interval expressions are only available if either --posix or --re-interval is specified on the command line.
上次由 lujinke 在 2008-12-06 19:35,总共编辑 3 次。
头像
princelai
帖子: 920
注册时间: 2007-01-06 21:00
联系:

Re: gawk '/^[A-Z][a-z]{3}/{print}‘怎么匹配不到Sjjj

#4

帖子 princelai » 2008-12-06 17:01

2楼是regex的高手阿
头像
goodluck1982
帖子: 171
注册时间: 2007-07-05 2:48

Re: gawk '/^[A-Z][a-z]{3}/{print}‘怎么匹配不到Sjjj

#5

帖子 goodluck1982 » 2008-12-08 10:43

回复3楼

这个只是 gawk 的特性,与grep等无关,
而且关于[A-Z]的理解并非gawk的bug似乎是有意这样实现的
每个支持正则表达式的程序都有一套关于正则表达式的自己的定义
整体上虽说一样,但细节上或许不同
用时需要注意
头像
goodluck1982
帖子: 171
注册时间: 2007-07-05 2:48

Re: gawk '/^[A-Z][a-z]{3}/{print}‘怎么匹配不到Sjjj

#6

帖子 goodluck1982 » 2008-12-08 10:45

princelai 写了:2楼是regex的高手阿
高手不敢当
只是善于google,善于man罢了
头像
eexpress
帖子: 58428
注册时间: 2005-08-14 21:55
来自: 长沙

Re: gawk '/^[A-Z][a-z]{3}/{print}‘怎么匹配不到Sjjj

#7

帖子 eexpress » 2008-12-08 10:57

[A-Za-z]?
● 鸣学
头像
goodluck1982
帖子: 171
注册时间: 2007-07-05 2:48

Re: gawk '/^[A-Z][a-z]{3}/{print}‘怎么匹配不到Sjjj

#8

帖子 goodluck1982 » 2008-12-08 14:49

楼上想说什么?
头像
eexpress
帖子: 58428
注册时间: 2005-08-14 21:55
来自: 长沙

Re: gawk '/^[A-Z][a-z]{3}/{print}‘怎么匹配不到Sjjj

#9

帖子 eexpress » 2008-12-08 15:13

☎ dog /boot/grub/grub.cfg |grep -o '[A-Za-z]\{7\}'
default
timeout
unicode
gfxmode
gfxterm
termina
gfxterm
backgro
desktop
highlig
magenta
highlig
你想说什么哦。呵呵
● 鸣学
头像
goodluck1982
帖子: 171
注册时间: 2007-07-05 2:48

Re: gawk '/^[A-Z][a-z]{3}/{print}‘怎么匹配不到Sjjj

#10

帖子 goodluck1982 » 2008-12-08 21:09

还是不明白楼上到底想说明什么问题。

代码: 全选

[12-06 18:24> ~]#  cat /boot/grub/menu.lst |grep -o '[A-Za-z]\{7\}'
generat                                                            
anacond                                                            
changes                                                            
partiti                                                            
relativ                                                            
vmlinuz                                                            
version                                                            
version                                                            
default
timeout
splashi
hiddenm
vmlinuz
vmlinuz
Windows
rootnov
chainlo
[12-06 18:24> ~]# gawk  '/[A-Za-z]\{7\}/ {print}'  /boot/grub/menu.lst
[12-06 18:24> ~]# gawk  '/[A-Za-z]{7}/ {print}'  /boot/grub/menu.lst
[12-06 18:24> ~]# gawk  --posix '/[A-Za-z]{7}/ {print}'  /boot/grub/menu.lst
# grub.conf generated by anaconda
# Note that you do not have to rerun grub after making changes to this file
# NOTICE:  You have a /boot partition.  This means that
#          all kernel and initrd paths are relative to /boot/, eg.
#          kernel /vmlinuz-version ro root=/dev/sda12
#          initrd /initrd-version.img
default=0
timeout=5
splashimage=(hd0,6)/grub/splash.xpm.gz
hiddenmenu
        kernel /vmlinuz-2.6.25.14-108.fc9.i686 ro root=UUID=718df761-8b0a-45ca-a752-73bf8e0d099e rhgb quiet
        kernel /vmlinuz-2.6.25-14.fc9.i686 ro root=UUID=718df761-8b0a-45ca-a752-73bf8e0d099e rhgb quiet
title Windows
        rootnoverify (hd0,0)
        chainloader +1
[12-06 18:24> ~]#
头像
eexpress
帖子: 58428
注册时间: 2005-08-14 21:55
来自: 长沙

Re: gawk '/^[A-Z][a-z]{3}/{print}‘怎么匹配不到Sjjj

#11

帖子 eexpress » 2008-12-09 8:56

[A-Z][a-z]{3} 不是这样写的。这是2个因子了啊。
[A-Za-z]\{7\} 这才是正则啊。
● 鸣学
头像
goodluck1982
帖子: 171
注册时间: 2007-07-05 2:48

Re: gawk '/^[A-Z][a-z]{3}/{print}‘怎么匹配不到Sjjj

#12

帖子 goodluck1982 » 2008-12-09 15:58

eexpress 写了:[A-Z][a-z]{3} 不是这样写的。这是2个因子了啊。
[A-Za-z]\{7\} 这才是正则啊。
首先看你要匹配什么了,如LZ所说,匹配 Sjjj 的话
用 [A-Z][a-j]<重复3次> 应该可以才对
至于 <重复3次> 是用 {3} 还是 \{3\} ,不同程序中的定义是不一样的
grep 默认使用 \{3\}
而gawk 则使用 {3}

我想说的只是,即使你写对了正则表达式,
用gawk时如果不注意 2楼 中说的两点
依然会使人困惑的!
回复