Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Unix 如何使用sed命令删除匹配字符串中的行,直到下一个匹配_Unix_Awk_Sed_Grep - Fatal编程技术网

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

Unix 如何使用sed命令删除匹配字符串中的行,直到下一个匹配,unix,awk,sed,grep,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

我在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
cat file.txt
testing1
dummy
我有另一个文件test.txt,其中包含必须从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
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
短awk命令:

k=testing1-指示要跳过的节的键名 f=$1==[k]-在由键k表示的遇到部分上用真值设置标志f;否则-设置为false 输出:

[testing]  
This is testing  
I want to remove content

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

这种类型的任务正是创建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

嗨,RomanPerekhrest,我有一个字符串列表,它被放在一个文件中,每个字符串都将被传递给您给出的命令,但我没有得到所需的输出。这是我试过的方法。因为我在cat文件中;doawk-vk=$i'/^\[/{f=$1==[k]}!f'测试;done@Sai,它在您当前的输入示例上运行良好。检查您是否发布了实际输入content@Saiawk命令周围不太可能需要一个shell循环,如果需要,则任何shell循环都将使用错误的语法。请更新您的问题以显示您真正想要做的事情。您问了一个问题stion,得到了这个问题的答案,然后评论说你真正需要做的是其他事情,然后你接受了原来的答案,然后你更新了你的问题以显示你真正想做的事情,而这是你不会用这个答案去做的。不确定你现在想的事情会发生,但我最好的建议是此时,您可以将此问题还原为开始时的问题,然后发布包含新需求和新示例输入/输出的后续问题。