问题
Is it possible to use grep to high lite all of the text starting with:
mutablePath = CGPathCreateMutable();
and ending with:
CGPathAddPath(skinMutablePath, NULL, mutablePath);
Where there is an arbitary amount of text in between those two phrases?
NOTE: I have to use grep because I'm using BBEdit.
回答1:
You will need to use GNU grep
:
grep -oPz 'mutablePath = CGPathCreateMutable\(\);.*?(\n.*?)*.*?CGPathAddPath\(skinMutablePath, NULL, mutablePath\);' file
If you don't have GNU grep
, you could use pcregrep
to achieve the same thing:
pcregrep -M 'mutablePath = CGPathCreateMutable\(\);.*(\n|.)*CGPathAddPath\(skinMutablePath, NULL, mutablePath\);' file
回答2:
If you want to print the lines between and including these you could use:
perl -ne '/start line/ .. /end line/ and print'
回答3:
You can use sed instead like this:
sed -n '/mutablePath = CGPathCreateMutable();/,/CGPathAddPath(skinMutablePath, NULL, mutablePath);/p' infile
EDIT:
Not sure if -P
flag of grep is supported in BBEdit. If it is then you can use this:
grep -oP 'mutablePath = CGPathCreateMutable();\X*CGPathAddPath(skinMutablePath, NULL, mutablePath);/' infile
As per grep man page:
-P, --perl-regexp Interpret PATTERN as a Perl regular expression.
来源:https://stackoverflow.com/questions/13638546/use-grep-to-find-blocks-of-text-between-two-phrases-including-the-phrases