jq-从流中提取匹配文档的子集 [英] jq - extracting a subset of matching documents from a stream

查看:138
本文介绍了jq-从流中提取匹配文档的子集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下JSON消息.这是一条完整的消息.单个文件中有如此多的消息.此json消息是使用jq从未格式化的json消息生成的.

I have the following JSON message. This is a single complete message. There are so many messages like this in a single file. This json message was generated from a unformated json message using jq.

{
  "header": {
    "user": "baskar"
  },
  "requests": [
    {
      "first_name": "mike",
      "last_name": "mat"
    },
    {
      "first_name": "mike",
      "last_name": "mat"
    }
  ],
  "check": [
    "Y"
  ]
}
{
  "header": {
    "user": "baskar"
  },
  "message": {
    "header": {
      "user": "baskar"
    },
    "response": {
      "resultsList": {
        "result": [
          {
            "first_name": "mike1",
            "last_name": "mat"
          }
        ]
      },
      "errorMsg": null
    }
  }
}

我想对此进行一些过滤.例如,当我搜索first_name,mike1时,我应该在请求中获取标头和匹配的请求.以及响应消息中的匹配结果.因此,对于搜索字符串mike1,预期输出如下.

I would like to do some filtering on this. For example, when i search for first_name, mike1, I should get the header and the matching request inside request. Also the matching result inside the Response message. So, the output is expected as follows for the search string mike1.

{
  "header": {
    "user": "baskar"
  },
  "requests": [
    {
      "first_name": "mike1",
      "last_name": "mat"
    }
  ],
  "check": [
    "Y"
  ]
}
{
  "header": {
    "user": "baskar"
  },
  "message": {
    "header": {
      "user": "baskar"
    },
    "response": {
      "resultsList": {
        "result": [
          {
            "first_name": "mike1",
            "last_name": "mat"
          }
        ]
      },
      "errorMsg": null
    }
  }
}

基本上,我想过滤出请求数组中不匹配的请求和结果数组中不匹配的结果.

Basically, i want to filter out unmatched request inside the requests array and unmatched result inside the result array.

当前,我使用以下脚本从未格式化的json消息日志文件中获取格式化的json消息.

Currently, I use the following script to get the formatted json message from the unformatted json message log file.

 sed -n "/<SEARCH_STRING>/ s/.*Service - //p" $1/test.log* | jq . > ~/result.log

谢谢, Baskar.S

Thanks, Baskar.S

推荐答案

jq --arg key first_name \
   --arg value mike1 \
   'select(.message.response.resultsList.result[]?[$key]==$value) | .message' \
   <in.json

...仅返回结果列表包含mike1中至少一个first_name的消息内容.

...returns only message content where the result list contains at least one first_name of mike1.

这篇关于jq-从流中提取匹配文档的子集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆