bash - 如何在不同的行上打印多个图案
问题描述
我有一个想用 bash 处理的文件。可以使用 awk、sed 或 grep 或类似的。该文件在一行中有多次出现。我想提取这两次出现之间的所有内容,并将输出分别打印在单独的行上。
我已经尝试过使用这个:
cat file.txt | grep -o 'pattern1.*pattern2'
但这将打印从模式 1 到最后一个匹配模式 2 的所有匹配项。
$ cat file.txt
pattern1 this is the first content pattern2 this is some other stuff pattern1 this is the second content pattern2 this is the end of the file.
我想得到:
pattern1 this is the first content pattern2
pattern1 this is the second content pattern2
解决方案
尝试 gnu sed:
sed -E 's/(pattern2).*(pattern1)(.*\1).*/\1\n\2\3/' file.txt
推荐阅读
- sql - 整月缺席-SQL DBMS
- scala - Kafka 消费者(相同的 group-id)总是从同一个分区读取
- babeljs - Webpack 报告我在 .eslintrc.json 文件中配置的 linter 错误
- ruby-on-rails - Ruby on rails 警告“ruby 正在以不安全的方式加载 libcrypto”
- r - 我想过滤物种名称“versicolor”的物种列的数据,我还想只选择“sepel_length”和“sepel_width”列
- oracle - 甲骨文顶点。加载没有 PK 的 CSV
- javascript - 根据国家/地区名称汇总对象中的数字(反应,javascript)
- c# - 在 ASP.NET 核心 ef 中更新值 (PUT) 时出现问题
- php - 使用 php laravel 填写 PDF 表单
- wix - MSI 显示违规进程未加载的 DLL 的文件正在使用对话框