使用 sed 删除两个模式(不包括)之间的线条 [英] removing lines between two patterns (not inclusive) with sed
本文介绍了使用 sed 删除两个模式(不包括)之间的线条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
好的
我知道这是一个微不足道的问题,但是:如何从两个已知模式/单词之间的文件中删除行:
I know that this is trivial question but: How can i remove lines from files that are between two known patterns/words:
模式1
垃圾
模式2
pattern1
garbage
pattern2
获得:
模式1
模式2
有没有人知道学习 sed 的好(简单写!)资源??有很多清晰的例子吗?
And does anyone known good(simple written!) resources for studying sed?? With many clear examples?
推荐答案
sed -n '/pattern1/{p; :a; N; /pattern2/!ba; s/.*
//}; p' inputfile
说明:
/pattern1/{ # if pattern1 is found
p # print it
:a # loop
N # and accumulate lines
/pattern2/!ba # until pattern2 is found
s/.*
// # delete the part before pattern2
}
p # print the line (it's either pattern2 or it's outside the block)
某些版本的 sed
必须用勺子喂食:
Some versions of sed
have to be spoon-fed:
sed -n -e '/pattern1/{' -e 'p' -e ':a' -e 'N' -e '/pattern2/!ba' -e 's/.*
//' -e '}' -e 'p' inputfile
这篇关于使用 sed 删除两个模式(不包括)之间的线条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文