grep包括换行符在内的所有字符 [英] grep all characters including newline
问题描述
我用
来分析XML文件lalala这是一个星期天{{这里有些词,也许
a new line}} oh boy
如何使用grep将{{和}}假设grep 。
字符不能识别换行符?
p>
grep'{{。*}}'
但它只适用于同一行上的东西。
是删除换行符然后grep,如下所示:
cat myfile | tr -d'\\\
'| grep {{。*}}
但是如果你说这是一个XML文件,为什么不使用XML分析器,它利用了文件的内在结构而不仅仅是正则表达式?
编辑
grep regexp很贪心,你可以用perl regexp:
cat myfile | tr -d'\\\
'| perl -pe's /.*?({{.*?}})/ \1\\\
/ g'| grep {{
这应该每行输出一个匹配项。如果你嵌套{{那么这将变得更加复杂。
I'm parsing an XML file with
"lalala it's a Sunday {{ Some words here, maybe
a new line }} oh boy"
How would I use grep to get everything within "{{" and "}}" given that the grep .
character doesn't recognize newlines?
Currently I have
grep '{{.*}}'
but it only works on things that are on the same line.
One option is to remove the newline and then grep, as in:
cat myfile | tr -d '\n' | grep {{.*}}
But if you say this is an XML file, why not use an XML parser that takes advantage of the file's inherent structure rather than just regexp?
EDIT
Grep regexp are greedy, you can use perl regexp:
cat myfile | tr -d '\n' | perl -pe 's/.*?({{.*?}})/\1\n/g' | grep {{
This should output one match per line. If you have nested {{ then this will get even more complicated.
这篇关于grep包括换行符在内的所有字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!