我可以搜索 2 个模式并将它们并排列出吗?

Cur*_*Sam 3 grep json

Ubuntu 16.04

bash -version  
GNU bash, version 4.4.0(1)-release (x86_64-unknown-linux-gnu)
Run Code Online (Sandbox Code Playgroud)

我想要grep2 个模式,然后将它们并排列出。目前,这就是我所拥有的:

root@tires ~ # grep -e tire_id -e appID /path/to/*/vehicle/production.json
/path/to/000001_000002/vehicle/production.json:    "tire_id": "1305436516186552",
/path/to/000001_000002/vehicle/production.json:        "appID": "1164562920689523",
/path/to/000001_000079/vehicle/production.json:    "tire_id": "1815123428733289",
/path/to/000001_000079/vehicle/production.json:        "appID": "18412365908966538",
/path/to/000001_000088/vehicle/production.json:    "tire_id": "138477888324",
Run Code Online (Sandbox Code Playgroud)

这就是我想要的,尽管任何类似的东西实际上都可以。

root@tires ~ # grep -e tire_id -e appID /path/to/*/vehicle/production.json
/path/to/000001_000002/vehicle/production.json:    tire_id: 1305436516186552, appID: 1164562920689523
/path/to/000001_000079/vehicle/production.json:    tire_id: 1815123428733289, appID: 18412365908966538
Run Code Online (Sandbox Code Playgroud)

文件示例在这里:

{
    "socal": "https://xxx.xxxxx.xxx",
    "ip": "xxx.xxx.xxx.xxx",
    "tire_id": "213275925375485",
    "client": {
        "platform": "xx",
        "clientID": "xxxxx",
        "serviceID": "xxxxx",
        "service_id": XXXX,
        "vendor": "default"
    },
    "locale": "en_US",
    "cdc": {
        "appID": "233262274090443",
        "isdel": "ORdiZBMAQS2ZBCnTwZDZD",
    },
    "attachments": {
        "output": "attachments",
        "public": false,
    },
}
Run Code Online (Sandbox Code Playgroud)

Rom*_*est 12

jq使用有效 JSON 文档的工具的正确方法:

样品file1.json

{
    "socal": "https://xxx.xxxxx.xxx",
    "ip": "xxx.xxx.xxx.xxx",
    "tire_id": "213275925375485",
    "client": {
        "platform": "xx",
        "clientID": "xxxxx",
        "serviceID": "xxxxx",
        "service_id": "XXXX",
        "vendor": "default"
    },
    "locale": "en_US",
    "cdc": {
        "appID": "233262274090443",
        "isdel": "ORdiZBMAQS2ZBCnTwZDZD"
    },
    "attachments": {
        "output": "attachments",
        "public": false
    }
}
Run Code Online (Sandbox Code Playgroud)

样品file2.json

{
    "socal": "https://xxx.xxxxx.xxx",
    "ip": "xxx.xxx.xxx.xxx",
    "tire_id": "1305436516186552",
    "client": {
        "platform": "xx",
        "clientID": "xxxxx",
        "serviceID": "xxxxx",
        "service_id": "XXXX",
        "vendor": "default"
    },
    "locale": "en_US",
    "cdc": {
        "appID": "1164562920689523",
        "isdel": "ORdiZBMAQS2ZBCnTwZDZD"
    },
    "attachments": {
        "output": "attachments",
        "public": false
    }
}
Run Code Online (Sandbox Code Playgroud)

以及解决方案本身:

jq -r 'input_filename + " tire_id: \(.tire_id) appID: \(.cdc.appID)"' file*.json
Run Code Online (Sandbox Code Playgroud)

输出:

file1.json tire_id: 213275925375485 appID: 233262274090443
file2.json tire_id: 1305436516186552 appID: 1164562920689523
Run Code Online (Sandbox Code Playgroud)