Grep正则表达式只能选择10个字符 [英] Grep regex to select only 10 character

查看:178
本文介绍了Grep正则表达式只能选择10个字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



日志文件的模式看起来像这样

 一些随机字符串....发送的消息[10个字符 -  

我只对 [ -

)之间存在的10个字符串感兴趣

我已经成功地设计了这个看起来像这样的正则表达式。

 Message Sent \ [ 。\ {10 \}  - 

我已经通过 here 。 但不幸的是,正则表达式不会产生任何输出

  grep -oMessage Sent \ [.\ {10 \}) - log / development.log 

但我最感兴趣的是10个字符,但是上面的正则表达式产量'Message Sent [' + 10个字符 + -



任何线索?

解决方案

使用 \K (来自Perl-regex的模式 )放弃先前匹配的字符。
$ b

  grep -oP'Message Sent \ [\K。{10}(?=  -  +)'log / development.log 

  grep -oP'Message \s + Sent\s + \ [\K。{10}(?= \s +  -  +)'log / development.log 

DEMO


I have a log file that I'm grep(ing) to select certain character.

The pattern of the log file looks like this

Some Random string.... Message Sent [10 character --

What I'm interested in only the 10 character string present between [ and --

I have successfully managed to compose the regex that look like this

"Message Sent \[.\{10\} --" 

I have cross checked the above regex over here.

But unfortunately the regex does not yield any output

grep -o "Message Sent \[.\{10\}) --" log/development.log

But I'm mostly interested in the 10 character but the above regex yield 'Message Sent [' + 10 character + --

Any Clue ?

解决方案

Use \K (pattern from Perl-regex) to discard the previously matched chars.

grep -oP 'Message Sent \[\K.{10}(?= -+)' log/development.log

or

grep -oP 'Message\s+Sent\s+\[\K.{10}(?=\s+-+)' log/development.log

DEMO

这篇关于Grep正则表达式只能选择10个字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆