我想捕获最后一个单词之前有 (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
答案1
搜索(4-digits)<spaces>word
行尾包含模式的行
grep -E '\([0-9]{4}\)\s*\w+$'
答案2
尝试
/opt/OV/bin/opcagt -status | awk 'NF>2 && $(NF-1) ~ /\([0-9]*\)/ '
在哪里
$(NF-1)
最后一个字段之前的匹配~
让 awk 进行模式匹配/\([0-0]*\)/
模式是(
,任意数量的数字并且)
(您可以使用 [0-9][0-9]* 来获得至少一个。- 默认操作是打印。
答案3
你可以做:
... | grep -E '[[:blank:]]\([0-9]{4}\)[[:blank:]]+[^[[:blank:]]+$'
[[:blank:]]\([0-9]{4}\)
匹配一个空格,后跟 4 位数字[[:blank:]]+[^[[:blank:]]+$
匹配末尾的一个或多个空格,后跟一个或多个非空格字符。
例子:
$ cat file.txt
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
$ grep -E '[[:blank:]]\([0-9]{4}\)[[:blank:]]+[^[[:blank:]]+$' file.txt
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