2016-11-22 75 views
1

不像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 
+0

無法重現。 GNU Awk 4.1.4會生成你想要的輸出。 – infotoni91

+2

在RHEL 5和GNU的awk 3.1.5,你必須使用'--posix'選項。 – Jdamian

+0

@Jdamian,謝謝,我最近幾個小時都在撓頭。 。 –

回答

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. 
+1

這是AWK版本3及更早版本的聯機幫助頁。在版本4中,默認情況下支持間隔表達式。 –