regex - Grep for word and line before match

标签 regex linux bash awk grep

我有一个文本日志文件,其中包含多个条目,如下所示:

Processing input.jpg (323 of 500)...
Detecting matches in region 1...
Detecting matches in region 2...
Detecting matches in region 3...
Detecting matches in region 4...
Detecting matches in region (n)...
...
NOT ENOUGH MATCHES - FULL FILE OUTPUT
Processing input1.jpg (324 of 500)...

我想对文件进行 grep 以匹配出现 FULL FILE 序列的每个实例,然后获取生成该结果的文件的名称 - 即在每个 FULL FILE 匹配之前找到开始处理的行。

如何使用 grep 或其他工具(如 sed 或 awk)执行此操作?

到目前为止,我能够匹配在日志中找到 FULL FILE 的每个实例并对其进行计数:

cat output.txt | grep "FULL FILE" | wc -l

但我现在需要从日志文件中为每个匹配项获取前面的文件名。

非常感谢任何帮助。

最佳答案

假设文件名中没有空格,awk 就可以了

awk '/^Processing/{file=$2};/FULL FILE OUTPUT$/{print file}' output.txt

关于regex - Grep for word and line before match,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27659360/

相关文章:

linux - pdo 驱动程序在 linux 中没有显示任何值

python - 如何从第一个终端打开第二个终端并在其中运行命令? (没有它是子进程)

node.js - npm 过时的输出颜色编码含义

java - 正则表达式 括号内的括号

regex - Sed 匹配数字后跟字符串并返回数字

php - 如何验证正则表达式本身?

linux - EPOLLET 的用例是什么?

bash - 继续从 tcp 服务器读取数据,直到收到特定消息

bash - stdoutput 和 stderror 的重定向不捕获所有输出

regex - MariaDB:带有 [.character.] 的 REGEX 不再起作用(“不支持 POSIX 整理元素”)