gpt4 book ai didi

linux - 在目录中使用换行符递归搜索文本?

转载 作者:太空宇宙 更新时间:2023-11-04 10:55:47 25 4
gpt4 key购买 nike

我有很多看起来像这样的大日志文件:

DATETIME ["2015-03-03 21:52"]
SERVER [{json_with_$_SERVER-Output}]
GET ["GET_JSON","AAA"]
POST ["POST_JSON","BBB","TEST1"]

DATETIME ["2015-03-03 21:53"]
SERVER [{json_with_$_SERVER-Output}]
GET ["GET_JSON","CCC"]
POST ["POST_JSON","DDD","TEST2"]

DATETIME ["2015-03-03 21:54"]
SERVER [{json_with_$_SERVER-Output}]
GET ["GET_JSON","AAA"]
POST ["POST_JSON","BBB","TEST3"]

DATETIME ["2015-03-03 21:55"]
SERVER [{json_with_$_SERVER-Output}]
GET ["GET_JSON","AAA"]
POST ["POST_JSON","EEE","TEST4"]

我想搜索大约 2 个关键字(它们之间是换行符)。 GET 行中的一个特定词和 POST 行中的一个特定词。

我需要这样的东西:

grep "GET(.*)AAA(.*)POST(.*)BBB"

正在搜索的内容:AAA(在 GET 行中)&& BBB(在 POST 行中)

预期的结果:

POST ["POST_JSON","BBB","TEST1"]

POST ["POST_JSON","BBB","TEST3"]

用哪些简单的方法可以做到这一点?

最佳答案

对第三个参数使用 GNU awk 来匹配 ():

$ find . -type f |
xargs gawk -v RS= 'match($0,/\nGET.*AAA.*\n(POST.*BBB.*)/,a){print a[1]}'
POST ["POST_JSON","BBB","TEST1"]
POST ["POST_JSON","BBB","TEST3"]

如果你真的想要输出行之间有一个空行,请添加 -v ORS='\n\n'

关于linux - 在目录中使用换行符递归搜索文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28840046/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com