Text-Processing
grep + 正則表達式匹配位於最後一個單詞之前的單詞
我想擷取最後一個單詞之前有 (XXXX) 單詞的所有行
而 xxxxx - 是數字
/opt/OV/bin/opcagt -status scopeux Perf Agent data collector (5102) Running midaemon Measurement Interface daemon (5110) Running ttd ARM registration daemon Running perfalarm Alarm generator (5111) Running agtrep OV Discovery Agent AGENT,AgtRep (5520) Running coda OV Performance Core COREXT (5529) Running opcacta OVO Action Agent AGENT,EA (5427) Running opcle OVO Logfile Encapsulator AGENT,EA (5443) Running opcmona OVO Monitor Agent AGENT,EA Running opcmsga OVO Message Agent AGENT,EA (5435) Running opcmsgi OVO Message Interceptor AGENT,EA (5553) Running ovbbccb OV Communication Broker CORE (5352) Running ovcd OV Control CORE (5344) Running ovconfd OV Config and Deploy COREXT (5383) Running
我嘗試
/opt/OV/bin/opcagt -status | grep [0-9]
但是這個 grep 語法不能擷取最後一個單詞之前的單詞
預期成績:
scopeux Perf Agent data collector (5102) Running midaemon Measurement Interface daemon (5110) Running perfalarm Alarm generator (5111) Running agtrep OV Discovery Agent AGENT,AgtRep (5520) Running coda OV Performance Core COREXT (5529) Running opcacta OVO Action Agent AGENT,EA (5427) Running opcle OVO Logfile Encapsulator AGENT,EA (5443) Running opcmsga OVO Message Agent AGENT,EA (5435) Running opcmsgi OVO Message Interceptor AGENT,EA (5553) Running ovbbccb OV Communication Broker CORE (5352) Running ovcd OV Control CORE (5344) Running ovconfd OV Config and Deploy COREXT (5383) Running
嘗試
/opt/OV/bin/opcagt -status | awk 'NF>2 && $(NF-1) ~ /\([0-9]*\)/ '
在哪裡
$(NF-1)
在最後一個欄位之前匹配~
要求 awk 進行模式匹配/\([0-0]*\)/
模式是(
,任意數量的數字和)
(您可以使用$$ 0-9 $$$$ 0-9 $$* 至少有一個。- 預設操作是列印。
在行尾搜尋包含模式
(4-digits)<spaces>word
的行grep -E '\([0-9]{4}\)\s*\w+$'