首页 > 解决方案 > 删除搜索模式之间的文本/行

问题描述

删除整行的编码很容易,但我试图弄清楚如何在 reg 表达式之间删除。我写了一个更有趣的例子,而不是我正在使用的技术报告:

当前文本:

 cactus in the desert blooms @year round
 and almost all cactus plants have very sharp needles
 ------ that may hurt you if you get too close-----
 so stay away from the needles all @year and admire the 
     many colors in the buds @and flowers

 more data more data more data

 cactus in the desert blooms @year round
 and almost all cactus plants have very sharp needles
 so stay away from the needles all @year and admire the 
 data more data more data @year
 data more data more data more data @year
     many colors in the buds @and flowers
more data
 cactus in the desert blooms @year round
 and almost all cactus plants have very sharp needles
    so stay away from the needles all @year and admire the 
     many colors in the buds @and flowers

more data more data
more data more data
more data more data

在这种情况下,我将@year其用作初始 reg 表达式并@and用作第二个。我想删除 reg 表达式之间的删除部分和完整行。更喜欢使用 SED。

注意:任何后续实例都@year需要被忽略,直到@and找到。如果找到多个 的实例,@and则不应执行任何操作,因为之前没有 的实例@year

显示的所有示例都需要结果:

 cactus in the desert blooms and flowers

 more data more data more data

 cactus in the desert blooms and flowers
 more data more data more data

 cactus in the desert blooms and flowers
 more data
 cactus in the desert blooms and flowers

 more data more data
 more data more data
 more data more data

标签: awksed

解决方案


您能否完全根据您在 GNU 中显示的示例尝试以下操作awk。如果您的 Input_file 中没有任何空行,请尝试。

awk -v RS= '{sub(/@year.*@and/,"and")} 1' Input_file

显示的示例输出将是:

 cactus in the desert blooms and flowers

推荐阅读