日志文件的模式看起来像这样
一些随机字符串....发送的消息[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.logDEMO
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.logBut 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.logor
grep -oP 'Message\s+Sent\s+\[\K.{10}(?=\s+-+)' log/development.logDEMO
更多推荐
Grep正则表达式只能选择10个字符
发布评论