Regex grep中的动态匹配(双grep)-反向引用?

Regex grep中的动态匹配(双grep)-反向引用?,regex,grep,Regex,Grep,从该输出中: 2017-01-06 15:28:21,384 INFO [py.com.interceptors.SecurityInterceptor] (default task-14) (#1dfp) {"id":"1dfp","method":"getImageById","actionURL":"/image/get/{imageId}","admin":"mgarcia"} 2017-01-06 15:28:21,384 INFO [py.com.SomeOthe

从该输出中:

    2017-01-06 15:28:21,384 INFO  [py.com.interceptors.SecurityInterceptor] (default task-14) (#1dfp) {"id":"1dfp","method":"getImageById","actionURL":"/image/get/{imageId}","admin":"mgarcia"}
    2017-01-06 15:28:21,384 INFO  [py.com.SomeOtherClass] (default task-14) (#1dfp) something I don't even know why I wanna log 
more lines that not necessary have the same format
    2017-01-06 15:28:21,384 INFO  [py.com.SomeOtherClass] (default task-14) (#1111) something I don't even know why I wanna log 
more lines that not necessary have the same format
    2017-01-06 15:28:21,384 INFO  [py.com.SomeOtherClass] (default task-14) (#1111) something I don't even know why I wanna log
    2017-01-06 15:28:21,384 INFO  [py.com.SomeOtherClass] (default task-14) (#1dfp) something I don't even know why I wanna log
我想得到:

2017-01-06 15:28:21,384 INFO  [py.com.interceptors.SecurityInterceptor] (default task-14) (#**1dfp**) {"id":"1dfp","method":"getImageById","actionURL":"/image/get/{imageId}","admin":"mgarcia"}
2017-01-06 15:28:21,384 INFO  [py.com.SomeOtherClass] (default task-14) (#1dfp) something I don't even know why I wanna log
2017-01-06 15:28:21,384 INFO  [py.com.SomeOtherClass] (default task-14) (#1dfp) something I don't even know why I wanna log
所有那些有(#1dfp)的行,基本上只有我不知道值:(#1dfp),直到我执行grep'/image/get/{imageId}'

可以在一行中完成吗?比如:

tail-f$log | grep'一些反向引用,或者通过/image/get/{imageId}获取所有(#1dfp)


谢谢

您可以为此使用
awk

awk -F '[()]' 'index($0, "/image/get/{imageId}"){id=$4} $4 == id' file.log

2017-01-06 15:28:21,384 INFO  [py.com.interceptors.SecurityInterceptor] (default task-14) (#1dfp) {"id":"1dfp","method":"getImageById","actionURL":"/image/get/{imageId}","admin":"mgarcia"}
2017-01-06 15:28:21,384 INFO  [py.com.SomeOtherClass] (default task-14) (#1dfp) something I don't even know why I wanna log
2017-01-06 15:28:21,384 INFO  [py.com.SomeOtherClass] (default task-14) (#1dfp) something I don't even know why I wanna log
工作原理:

  • 我们使用
    作为输入字段分隔符
  • index($0,“/image/get/{imageId}”)
    找到一行文本
    /image/get/{imageId}
    ,然后
    id=$4
    将第四个字段分配给
    id
  • 然后,
    $4==id
    只打印所有与变量
    id
    相同的
    $4

你能多发几行文章来获得一个清晰的例子吗?为什么你的文件不起作用?你把两个grep合并是什么意思?也许你可以给出更多的输出示例行?@RomanPerekhrest谢谢你的回复,刚刚更新了Q@LarsFischer感谢您的回复,刚刚更新了QQ从包含
/image/get/{imageId}
的行中获取
(#1dfp)
的规则是什么?是否总是在括号内的单词或其他东西后面的括号内的第二个单词?尝试过,但不起作用,因为有些行不符合确切的模式…答案将与所提供的信息一样好。你应该用更多的细节或者更多的样本来更新这个问题。你是对的。刚刚更新了Q。我将添加更多输出,只是不想让Q长得像地狱一样,但它也可以包含stacktraces和stuffMy上面给定的awk命令仍在处理更新的示例数据。