2010-08-11 59 views
1

我想提取兩個圖案之間的可變的位數,例如:提取數字的awk/egrep的

校正:

blah blah.... AAM #6,blah blah 

blah blah.... AAM #10 , blah blah 

blah blah.... AAM #100 , blah blah 

輸出:6,10和100

我需要提取AMA #,之間的數字

回答

0

假設這兩種模式的數字應該是之間是AAM#,

gawk 'match($0, /AAM #([[:digit:]]+)[[:space:]]*,/, a) {print a[1]}'

+0

這種運作良好。謝謝!! – aapl 2010-08-11 22:54:10

+0

我試圖將其用作較大搜索的一部分,並遇到錯誤。 cmd是sed -n「/ COMPLETE/p」1.txt | gawk'BEGIN {FS =「/」} {printf「%s%s:%s \ n」,substr($ 4,1,10),substr($ 6,8,1),match($ 7,/ AAM#( [[:digit:]] +)[[:space:]]?,/,a)a [1]}'我試圖讓數字進入printf的3%s,並且看到數字前面有一個計數(2或3)。任何想法我的錯誤是什麼? – aapl 2010-08-11 23:40:22

0

嘗試像(?< =#)\ d +

0

沒有必要,如果你用awk使用SED。它是多餘的。

gawk 'BEGIN{FS="/"}/COMPLETED/{ 
    match($7, /AAM #([[:digit:]]+)[[:space:]]?,/, a) 
    printf "%s %s:%s \n", substr($4,1,10),substr($6,8,1), a[1] 
} ' file 
+0

更好。自從我編寫腳本以來,已經有15年了。試過以上,並在printf有一個語法錯誤。像這樣添加{},這會產生不正確的結果。 gawk'BEGIN {FS =「/」}/COMPLETED/{match($ 7,/ AAM#([[:digit:]] +)[[:space:]]?,/,a)} {printf「 %s%s:%s \ n「,substr($ 4,1,10),substr($ 6,8,1),a [1]}'文件 – aapl 2010-08-12 02:02:40

+0

發佈腳本@ ghostdog74時沒有語法錯誤,必須弄亂複製/粘貼。通過添加這些圓括號,您可以將printf移動到塊的外部,只有在找到COMPLETED時才執行該塊,並顯着改變腳本。 – 2012-11-02 14:46:00

1
$ cat file 
blah blah.... AAM #6,blah blah 
blah blah.... AAM #10 , blah blah 
blah blah.... AAM #100 , blah blah 

$ awk -F'(AAM #| *,)' '{print $2}' file 
6 
10 
100