2012-09-27 76 views
0

我有其在系統地址讀取一條線,其中可以存在01-47之間一個通配符號(在特定的地址將是像環??。host.com)快速AWK格式問題

我試圖讓AWK使用正則表達式

識別的條件論證任何這些數字的

到目前爲止,這是我想出了,但它似乎是不正確的

/ring[0-4][0-7].host.com/{ (rest of blah blah blah goes here)} 

我知道,這可能給我值00,但我可以安全地假設在這種情況下,它將在01-47連接。

在此先感謝您的幫助!

回答

1

你可以使用一些花哨的正則表達式,或者你可以簡單地測試如果數字不等於「00」和小於或等於47. GNU awk,或許你可以嘗試這樣的事:

awk -r '/ring[0-9]{2}\.host\.com/ { string = substr($0, 5, 2); if (string != "00" && string <= 47) print }' file.txt 
0

如果 「00」 爲你操心,唯一的情況怎麼樣:

awk '/ring[0-4][0-7]\.host\.com/ && !/ring00\.host\.com/{blahblah}' 

例如:

kent$ echo "ring00.host.com 
dquote> ring01.host.com 
dquote> ring10.host.com"|awk '/ring[0-4][0-7]\.host\.com/ && !/ring00\.host\.com/' 
ring01.host.com 
ring10.host.com 

如果「39」也是有效的,你的正則表達式將不起作用。你必須提取2digits並做一些像史蒂夫那樣的事情。

1

如果你想要嚴格01至47:(0[1-9]|[123][0-9]|4[0-7])

+0

+1我會記住這一點! – Steve

+0

非常感謝!我發現我的核心問題是愚蠢的(忘了一個字段標記爲$),但這幫助我了錯誤陷阱! –