Use Grep to find blocks of text between two phrases (including the phrases)

独自空忆成欢 提交于 2020-01-04 02:46:30

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!