awk 版本 3.1.7 中的字符 class 范围
character class range in awk version 3.1.7
与grep
不同,我无法在`awk 中定义数字字符class 的size/range。任何正确方向的线索都值得赞赏。
cat input
1abc
12abc
123abc
1234abc
12345abc
在grep
中我可以定义数字字符的size/lengthclass
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
注二:
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.
与grep
不同,我无法在`awk 中定义数字字符class 的size/range。任何正确方向的线索都值得赞赏。
cat input
1abc
12abc
123abc
1234abc
12345abc
在grep
中我可以定义数字字符的size/lengthclass
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
注二:
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.