![每个 grep 结果到不同的文件](https://linux22.com/image/90431/%E6%AF%8F%E4%B8%AA%20grep%20%E7%BB%93%E6%9E%9C%E5%88%B0%E4%B8%8D%E5%90%8C%E7%9A%84%E6%96%87%E4%BB%B6.png)
我有类似的东西grep "keyword" -B 3 log
。它显示了很多结果,例如:
some trash
[GET] /orders/42
one more trash
keyword and useful info
[GET] /orders/43
some trash
one more trash
keyword and useful info
some trash
one more trash
[GET] /orders/44
keyword and useful info
如何使用line.txt 将每个grep
结果保存到单独的文件中?每个结果保证有一行。42, 43, 44
keyword and useful info
grep
[GET] /orders/[0-9]*
答案1
awk -v keyword=keyword -F/ '/\[GET\]/ { id=$NF; next } $0 ~ keyword { print $0 > id }' log