Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Opposite of "only-matching" in grep?

Tags:

grep

Is there any way to do the opposite of showing only the matching part of strings in grep (the -o flag), that is, show everything except the part that matches the regex?

That is, the -v flag is not the answer, since that would not show files containing the match at all, but I want to show these lines, but not the part of the line that matches.

EDIT: I wanted to use grep over sed, since it can do "only-matching" matches on multi-line, with:

cat file.xml|grep -Pzo "<starttag>.*?(\n.*?)+.*?</starttag>"
like image 375
Samuel Lampa Avatar asked Feb 19 '23 06:02

Samuel Lampa


1 Answers

This is a rather unusual requirement, I don't think grep would alternate the strings like that. You can achieve this with sed, though:

sed -n 's/$PATTERN//gp' file

EDIT in response to OP's edit:

You can do multiline matching with sed, too, if the file is small enough to load it all into memory:

sed -rn ':r;$!{N;br};s/<starttag>.*?(\n.*?)+.*?<\/starttag>//gp' file.xml
like image 174
Lev Levitsky Avatar answered May 08 '23 22:05

Lev Levitsky