Issue
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 ?
Solution
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
Answered By - Avinash Raj