sed匹配换行符中的模式 [英] sed to match pattern across a newline

查看:149
本文介绍了sed匹配换行符中的模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的输入内容

<array>
    <string>extra1</string>
    <string>extra2</string>
    <string>Yellow
5</string>

注意:黄色"和"5"之间有空格和换行符

Note: there's a space and newline between "Yellow" and "5"

我正在将此管道用于sed:

I am piping this to sed:

| sed -n 's#.*<string>\(.*\)</string>#\1#p'

我得到了输出:

extra1
extra2

我知道,因为sed从每个输入行的末尾剥离换行符,所以换行符不匹配-因此要考虑结果.我已经阅读了有关从缓冲区添加下一行的文章,但是我无法弄清楚在模式匹配中需要使用什么来使它起作用.

I know that, because sed strips the newline from the end of each input line, the newline is not there to be matched - so that accounts for the result. I have read articles on adding the next line from the buffer, but I can't work out what I need to use in the pattern match to get this to work.

我想要的输出是:

extra1
extra2
Yellow 5

(如果有所作为,我使用的是Mac,因此我需要使用它-我认为-sed的FreeBSD变体.)

(In case it makes a difference, I am using a Mac, so I need this to work with - I think - the FreeBSD variant of sed.)

当然,如果其他工具更适合我想要实现的目标,我欢迎您提出建议!谢谢!

Of course, if another tool is better for what I want to achieve I am open to suggestions! Thanks!

推荐答案

加入这些行并将它们分开:

Join the lines and tear them apart:

tr -d "\n" < file| grep -o "<string>[^<]*</string>"|sed 's/<string>\(.*\)<\/string>/\1/'

这篇关于sed匹配换行符中的模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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