shell - 如何仅提取bash中两个字符串之间多行的第一个实例?
问题描述
我的文件是:
abc
123
xyz
abc
675
xyz
我想提取:
abc
123
xyz
(123 可以是任何东西,关键是我想要第一次出现)
我尝试使用这个:
sed -n '/abc/,/xyz/p' filename
但这给了我所有的例子。我怎么能得到第一个?
解决方案
您能否尝试使用显示的示例进行以下、编写和测试。
awk '/abc/{found=1} found; /xyz/ && found{exit}' Input_file
或根据 Ed sir 的评论以提高效率,请尝试关注。
awk '/abc/{found=1} found{print; if (/xyz/) exit}' Input_file
说明:为上述添加详细说明。
awk ' ##Starting awk program from here.
/abc/{ ##checking condition if a line has abc in it then do following.
found=1 ##Setting found here.
}
found; ##Checking condition if found is SET then print that line.
/xyz/ && found{ ##Checking if xyz found in line and found is SET then do following.
exit ##exit program from here.
}
' Input_file ##Mentioning Input_file name here.
推荐阅读
- javascript - while 循环为世界旅行创建一个数组
- flutter - 由于缺少 pubspec.yaml,无法在 Android Studio 中创建 Flutter 项目
- angular - 如何通过 Angular 和 Spring boot 显示图像文件?
- nvidia - 如何获取使用张量核心的 CUDA 内核的完整列表?
- python - 如何使用while循环输出列表?
- nim-lang - 模板中的 Nim 可变参数?
- spring - 在对用户进行身份验证时,我可以在标头中将第一个 jwt 以及在正文中的相关数据发送回吗?
- mysql - 基于开始日期和频率的重复检查的数据库结构
- tensorflow.js - 使用 tensorflowjs 进行文本检测
- grafana - Grafana Cloud:划分两个查询显示“无数据”