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'
If you have GNU
grep you can use perl compatible regular expressions:
grep -oP 'p1\K.*?(?=p2)' filename
\Kresets the match after
p1has been matched. That would prevent
p1from getting included into the match result.
.+?matches one or more arbitrary characters - ungreedy, meaning only until the nearest occurrence of
(?=p2)is lookahead assertion. It means that the previous pattern, which was
.+?needs to get followed by