我有类似的东西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