I've got problem with removing part of string between two patterns with sed. I've always got last PATTERN-2 in line:
test.txt:
PATTERN-1xxxxPATTERN-2aaa
PATTERN-1xxxxPATTERN-2fffPATTERN-1zzzzPATTERN-2gggPATTERN-1zzzzPATTERN-2
PATTERN-1xxxxPATTERN-2bbb
cmd
sed 's/PATTERN-1.*PATTERN-2//g' test.txt
the result of above is
aaa
bbb
but I would like to have
aaa
fffggg
bbb
Is possible to find PATTERN-2 which is closest to PATTERN-1?
Best Answer
As @steeldriver points out, it is easy if you have non-greedy regexps. If not, you can do it with a loop, like this:
This works because we know there are no newlines in the middle of any line. It would also work with any other character that does not occur in any line, e.g.
ยง
.