awk - 删除搜索模式之间的文本/行
问题描述
删除整行的编码很容易,但我试图弄清楚如何在 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
解决方案
您能否完全根据您在 GNU 中显示的示例尝试以下操作awk
。如果您的 Input_file 中没有任何空行,请尝试。
awk -v RS= '{sub(/@year.*@and/,"and")} 1' Input_file
显示的示例输出将是:
cactus in the desert blooms and flowers
推荐阅读
- javascript - 表单不显示在 Angular 中
- react-native - 在 ScrollView 中使用 ListItem、FlatList 时出现警告
- python - Python PyAutoGui pixelMatchesColor 引发 windll.user32.ReleaseDC 错误
- flutter - 在按下按钮两次之前状态不会更新
- python - 无论线程如何,tkinter 都会冻结
- performance - Rust 编译中最耗时的部分通常是什么?
- powershell - 如何使用下面的 Try/catch 块实际捕获无法找到的电子邮件地址?
- c++ - 运行时错误:基数为 0x000000000000 的指针索引表达式溢出到 0xffffffffffffffe8
- javascript - 如何在 Three.js 中设置 Orbit Control 的水平旋转限制
- javascript - 如何分离或提取 HTML 标签?