You are not logged in.

#1 2017-06-25 21:36:30

TJM
Member
Registered: 2016-09-18
Posts: 118

[Perl regex] lookbehind assertion must be fixed length?

Hi there,

Usually, when I want to filter strings with following pattern:

  • a substring 'I/O error' is in front of another substring with pattern 'sector a int here'

  • a few substrings are between 'I/O error' and 'sector'

Usually, I use two piped grep:

 grep 'I/O error' /path/to/file | grep -E 'sector\s[[:digit:]]+'

But I want to know if I can do it within one regex expression with 'grep -P'.
I tried

 grep -P '(?<=I/O\serror.+)sector\s\d+'  /path/to/file 

but it got an error

 grep: lookbehind assertion is not fixed length 

So is it possible to do is using only one regex expression?

Offline

#2 2017-06-25 22:05:55

Raynman
Member
Registered: 2011-10-22
Posts: 1,539

Re: [Perl regex] lookbehind assertion must be fixed length?

You can simply combine them:

grep -E 'I/O error.+sector\s[[:digit:]]+'

Or with PCRE to exclude the first part from the match:

grep -P 'I/O error.+\Ksector\s[[:digit:]]+'

Offline

#3 2017-06-25 23:10:18

TJM
Member
Registered: 2016-09-18
Posts: 118

Re: [Perl regex] lookbehind assertion must be fixed length?

Raynman wrote:

You can simply combine them:

grep -E 'I/O error.+sector\s[[:digit:]]+'

Or with PCRE to exclude the first part from the match:

grep -P 'I/O error.+\Ksector\s[[:digit:]]+'

Thanks for your reply, but the problem is that it will match the 'I/O error', I want some expression that only match 'sector\s[[:digit:]]' so I won't get useless message if I use '-Po'

Offline

#4 2017-06-26 07:55:53

Raynman
Member
Registered: 2011-10-22
Posts: 1,539

Re: [Perl regex] lookbehind assertion must be fixed length?

The second one should do that? That \K escape sequence is actually mentioned in the pcrepattern man page as a way to get around the fixed-length restriction of lookbehind assertions.

Offline

#5 2017-06-26 20:32:56

TJM
Member
Registered: 2016-09-18
Posts: 118

Re: [Perl regex] lookbehind assertion must be fixed length?

Raynman wrote:

The second one should do that? That \K escape sequence is actually mentioned in the pcrepattern man page as a way to get around the fixed-length restriction of lookbehind assertions.

It works, thank you.

Offline

Board footer

Powered by FluxBB