Issue
I know this type of search has been address in a few other questions here, but for some reason I can not get it to work in my scenario.
I have a text file that contains something similar to the following patter:
some text here done
12345678_123456 226-
more text
some more text here done
12345678_234567 226-
I'm trying to find all cases where done
is followed by 226-
on the next line, with the 16 characters proceeding. I tried grep -Pzo
and pcregrep -M
but all return nothing.
I attempted multiple combinations of regex to take in account the 2 lines and the 16 chars in between. This is one of the examples I tried with grep
:
grep -Pzo '(?s)done\n.\{16\}226-' filename
Related posts:
- How to find patterns across multiple lines using grep?
- Regex (grep) for multi-line search needed [duplicate]
- How can I search for a multiline pattern in a file?
Solution
You must not escape {
and }
while using -P
(PCRE) option in grep
. That escaping is only for BRE.
You can use:
grep -ozP 'done\R.{16}226-\R' file
done
12345678_123456 226-
done
12345678_234567 226-
\R
will match any unicode newline character. If you are only dealing with \n
then you may just use:
grep -ozP 'done\n.{16}226-\n' file
Answered By - anubhava Answer Checked By - Candace Johnson (WPSolving Volunteer)