awk - Unix 提取两行之间的行并将它们存储在各自的文件中

我在文件 allreport.txt 中有一个如下所示的文件:

11:22:33:456        Script started                  Running: first_script
11:22:34:456        GetData - Read                  Read 12 Bytes
11:22:34:456        SetData - Write                 Write 12 Bytes
11:32:33:456        Script started                  Running: second_script
11:32:34:456        GetData - Read                  Read 12 Bytes
11:32:34:456        SetData - Write                 Write 12 Bytes
11:42:33:456        Script started                  Running: third_script
11:42:34:456        GetData - Read                  Read 12 Bytes
11:42:34:456        SetData - Write                 Write 12 Bytes
11:52:33:456        Script started                  Running: fourth_script

我的要求是我需要提取“*脚本”之间的“....”行。我试过类似下面的东西:

grep 'Running:' allreport.txt | sed 's/[^ ]* //' | cut -d":" -f2 | tr '\n' ' ' | awk -v col=1 '/$col/,/$($col+1)/ allreport.txt  > $col'

但是在执行命令后我没有看到任何输出,也没有创建结果文件?

我怎样才能达到同样的目的——预期的输出是像 first_script、second_script 等文件,每个文件都包含它的运行日志——例如 first_script 应该只有下面几行:

11:22:34:456        GetData - Read                  Read 12 Bytes
11:22:34:456        SetData - Write                 Write 12 Bytes

同样,second_script 应该有下面几行等等:

11:32:34:456        GetData - Read                  Read 12 Bytes
11:32:34:456        SetData - Write                 Write 12 Bytes

最佳答案

仅使用您显示的示例,请尝试执行以下操作。作为输出,它将创建 3 个名为 first_scriptsecond_scriptthird_script 的文件,其中包含示例。

awk -F': ' '/Running/{close(outFile);outFile=$2;next} {print > (outFile)}' Input_file

解释: 简单的解释是,将字段分隔符设为 : 然后检查行是否有 Running然后它将输出文件名设置为第二个字段。如果行没有 Running 则将该行打印到输出文件中。还要确保在后端关闭输出文件以避免此处出现“太多打开的文件错误”。

https://stackoverflow.com/questions/67806973/

相关文章:

git - 如何在 git pull 之前预览更改

kotlin - 如何获取数组 Kotlin 中特定值的大小

javascript - 使 href 中带有哈希 (#) 的每个 anchor 标记都不可点击

javascript - 如何使用对象从对象数组javascript中删除它?

python - Tensorflow Metal 插件已注册错误

javascript - 如何在reactjs中将csv文件数据转换为json对象?

google-cloud-platform - 请求中的资源字段值无效

javascript - React Native FlatList 刷新不起作用

awk - 从使用 sed 获得的部分中获取上面的 N 行

loops - 为什么内循环收集不返回结果?