RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 1115060
Accepted
Mikhail Murugov
Mikhail Murugov
Asked:2020-04-24 19:24:19 +0000 UTC2020-04-24 19:24:19 +0000 UTC 2020-04-24 19:24:19 +0000 UTC

从日志中获取完整的有效 json

  • 772

有一个系统Linux。它有一个日志。在此日志中有一个有效的json. 这json一个被分成几行。我确定这里面有一个值为 的json键。如何从此日志中获取包含此键和值的全部内容?阅读日志本身没有问题,只需将其拉出即可。 "id"12345jsonjson


我试过的:

grep -zo "{.*\"id\".*12345.*}" log.log
grep -ez "{.*\"id\".*12345.*}" log.log

不幸的是,我没有得到想要的结果。


测试日志示例:

2020-04-24 14:00:58.870+0300:Another string
2020-04-24 14:00:58.872+0300:Unusual json: {"some data": "another data"}
2020-04-24 14:00:58.891+0300:Answer from some service: {
 "event_id":"some id"
, "some information":"some value"
, "id":12345
, "timestamp":1587726058000
, "status":"ok"
,"some integer":100
, "category_scores":[
 {"id":"id1","value":123},
 {"id":"id2","score":234},
 {"id":"id3","score":100},
 {"id":"id4","score":299},
 {"id":"id5","score":600}
]
, "result":{"action":"some action","param":"0","action_code":"allow"}
, "service":{"request_id":1448
}}


从这个例子你应该得到:

{
 "event_id":"some id"
, "some information":"some value"
, "id":12345
, "timestamp":1587726058000
, "status":"ok"
,"some integer":100
, "category_scores":[
 {"id":"id1","value":123},
 {"id":"id2","score":234},
 {"id":"id3","score":100},
 {"id":"id4","score":299},
 {"id":"id5","score":600}
]
, "result":{"action":"some action","param":"0","action_code":"allow"}
, "service":{"request_id":1448
}}
linux
  • 1 1 个回答
  • 10 Views

1 个回答

  • Voted
  1. Best Answer
    Senior Pomidor
    2020-04-24T20:08:37Z2020-04-24T20:08:37Z

    假设我们有一个 log.log 文件,它包含您的文本

    然后命令看起来像这样

    cat log.log  | \
     sed -e "s/^\([0-9]\+\).*{/{/g" | \ 
     sed -e "s/^\([0-9]\+\).*//g" |  \
     tr -d '\n' \
     | jq " .| select(.id  == 12345)"
    
    1. 删除所有以数字开头且为 { 的行,留下符号 {
    2. 删除所有以数字开头的行
    3. 删除校正器以使其更容易标准化
    4. 我们只选择那些父参数.id为 的 JSON,其中值为 12345

    全部的

    {
      "event_id": "some id",
      "some information": "some value",
      "id": 12345,
      "timestamp": 1587726058000,
      "status": "ok",
      "some integer": 100,
      "category_scores": [
        {
          "id": "id1",
          "value": 123
        },
        {
          "id": "id2",
          "score": 234
        },
        {
          "id": "id3",
          "score": 100
        },
        {
          "id": "id4",
          "score": 299
        },
        {
          "id": "id5",
          "score": 600
        }
      ],
      "result": {
        "action": "some action",
        "param": "0",
        "action_code": "allow"
      },
      "service": {
        "request_id": 1448
      }
    }
    

    ps 但是如果日志太大,那么最好这样做sed -i filename

    • 1

相关问题

  • 如果 fuser -k number/tcp 没有帮助,如何在 Debian 中释放端口?

  • Ubuntu。startx 不起作用。黑屏

  • --syn 在 iptables 中有什么作用?

  • 为什么需要iso格式?

  • C程序中没有密码的sudo

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    如何从列表中打印最大元素(str 类型)的长度?

    • 2 个回答
  • Marko Smith

    如何在 PyQT5 中清除 QFrame 的内容

    • 1 个回答
  • Marko Smith

    如何将具有特定字符的字符串拆分为两个不同的列表?

    • 2 个回答
  • Marko Smith

    导航栏活动元素

    • 1 个回答
  • Marko Smith

    是否可以将文本放入数组中?[关闭]

    • 1 个回答
  • Marko Smith

    如何一次用多个分隔符拆分字符串?

    • 1 个回答
  • Marko Smith

    如何通过 ClassPath 创建 InputStream?

    • 2 个回答
  • Marko Smith

    在一个查询中连接多个表

    • 1 个回答
  • Marko Smith

    对列表列表中的所有值求和

    • 3 个回答
  • Marko Smith

    如何对齐 string.Format 中的列?

    • 1 个回答
  • Martin Hope
    Alexandr_TT 2020年新年大赛! 2020-12-20 18:20:21 +0000 UTC
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5