Amir Amf Amir Amf - 10 months ago 63
Linux Question

How to search for a text between two patterns in one line with multiple occurrences using sed?

I have a file like:

"This is a sample file to find text between p1 (the first pattern) and p2 (the second pattern) in one line, where we can have multiple occurrences of p1 followed by p2 and I only want the text between p1 and p2 in the same line."

I want to print the output like the following:
(the first pattern) and followed by and

I try the command

cat filename | sed -e 's/.*p1\(.*\)p2.*/\1/g'

but it only prints out the last one like:

Thanks in advance


If you have GNU grep you can use perl compatible regular expressions:

grep -oP 'p1\K.*?(?=p2)' filename


  • \K resets the match after p1 has been matched. That would prevent p1 from getting included into the match result.
  • .+? matches one or more arbitrary characters - ungreedy, meaning only until the nearest occurrence of p2
  • (?=p2) is lookahead assertion. It means that the previous pattern, which was .+? needs to get followed by p2.