字符类范围3.1.7
问题描述:
不像grep
我不能确定在'awk的数字字符类的大小/范围。任何线索正确的方向表示赞赏。字符类范围3.1.7
cat input
1abc
12abc
123abc
1234abc
12345abc
在grep
我可以定义位字符类
grep -P '^\d{3,4}' input #or grep -P '^[[:digit:]]{3,4}' input
123abc
1234abc
12345abc
grep -P '^\d{4,}' input #or grep -P '^[[:digit:]]{4,}' input
1234abc
12345abc
的大小/长度现在我想用awk来做到这一点,但同样的正则表达式是行不通的。
例如下面的命令不给任何输出。
awk '/^[[:digit:]]{3,4}/' input
awk '/^([[:digit:]]){3,4}/' input
我期待上面的命令打印
123abc
1234abc
12345abc
注1:目前我使用的界定范围,但它是不甜的大范围。
awk '/^[0-9][0-9]?[0-9]?/' input
注2:
awk --version |head -1
GNU Awk 3.1.7
答
使用--posix
选项。
在awk的第3版的手册页,你可以读到:
r{n,m} One or two numbers inside braces denote an interval expression. If there is one number in the braces, the preceding regu-
lar 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.
+1
这是AWK版本3及更早版本的联机帮助页。在版本4中,默认情况下支持间隔表达式。 –
无法重现。 GNU Awk 4.1.4会生成你想要的输出。 – infotoni91
在RHEL 5和GNU的awk 3.1.5,你必须使用'--posix'选项。 – Jdamian
@Jdamian,谢谢,我最近几个小时都在挠头。 。 –