1

不像grep我不能在`awk.d'中定义数字字符类的大小/范围。任何正确方向的线索都值得赞赏。

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
4

1 回答 1

3

使用该--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.
于 2016-11-22T07:41:09.360 回答