grep包括换行符在内的所有字符 [英] grep all characters including newline

查看:1149
本文介绍了grep包括换行符在内的所有字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用

 来分析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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆