Grep 提取匹配给定模式的单词 [英] Grep to extract the word matching the given pattern
本文介绍了Grep 提取匹配给定模式的单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个包含类似条目的日志文件
I have a log file with entries like
INFO 2013-08-16 13:46:48,660 Index=abc:12 insertTotal=11
INFO 2013-08-16 13:46:48,660 Index=abcd:12 insertTotal=11
INFO 2013-08-16 13:46:48,660 Index=def:134 insertTotal=11
INFO 2013-08-16 13:46:48,660 Index=abkfe insertTotal=11
INFO 2013-08-16 13:46:48,660 Index=lmkfe insertTotal=11
INFO 2013-08-16 13:46:48,660 Index=lmkfe insertTotal=11
我想 grep 并提取与我的模式匹配的单词,即 abc:<some_number>
和 def:<some_number>
.
I would like to grep and extract the words that match my pattern which is abc:<some_number>
and def:<some_number>
.
$ cat "log.txt" | grep -w "abc" -w "def" >> "failed_values.txt";
所以在这种情况下,我的failed_values.txt
应该只有
So in this case, my failed_values.txt
should only have
abc:12
def:134
要注意的关键是我的模式以 :
结尾,后跟一个数字,然后是一个空格 e.G.abc:122
.
The key to note is that my pattern ends with a :
followed by a number and then a space e. g. abc:122
.
推荐答案
尝试以下操作:
$ grep -Eio '[a-z]+:[0-9]+' log.txt
abc:12
abcd:12
def:134
-i
忽略大小写.-o
只打印匹配的部分.-i
to ignore case.-o
to print only matched part.
更新
只匹配abc
/def
:
$ grep -Eio '\b(abc|def):[0-9]+\b' log.txt
abc:12
def:134
(abc|def):
:匹配abc
或(|)def
后跟:
.[0-9]+
:匹配的数字.\b
:匹配词边界(abc|def):
: matchabc
or(|)def
followed by:
.[0-9]+
: matched numbers.\b
: match word boundary
这篇关于Grep 提取匹配给定模式的单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文