如何使用sed命令从匹配的字符串中删除行直到下一个匹配

时间:2018-03-30 13:08:23

标签: unix awk sed grep

我在文件content.txt中有以下内容

cat content.txt
[testing]  
This is testing  
I want to remove content

[testing1]  
This is testing1  
This is contains testing information  

[dummy]  
some text here as well  
this is a dummy file

[source]
This is the source file
it contains data related to source file

我有另一个文件“test.txt”,其中包含必须从文件“content.txt”中删除的字符串

cat file.txt
testing1
dummy

我需要从file.txt中读取每个字符串,并且应删除相同的字符串,然后删除下一行,直到在context.txt的方括号中找到下一个字符串

输出应如下:

[testing]  
This is testing  
I want to remove content  

[source]
This is the source file
it contains data related to source file

2 个答案:

答案 0 :(得分:1)

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 命令:

awk
  • awk -v k="testing1" '/^\[/{ f = ($1 == "["k"]") }!f' file - 指示要跳过的部分的键名
  • k="testing1" - 在遇到由键f = ($1 == "["k"]")表示的部分时设置标记f,其值为true;否则 - 使用k
  • 设置

输出:

false

答案 1 :(得分:0)

这种类型的任务正是创建awk的段落模式来处理的:

$ awk -v RS= -v ORS='\n\n' '$1 != "[testing1]"' file
[testing]
This is testing
I want to remove content

[dummy]
some text here as well
this is a dummy file