2つのパターンを見つけて並べて表示できますか?

2つのパターンを見つけて並べて表示できますか?

Ubuntu16.04

bash -version  
GNU bash, version 4.4.0(1)-release (x86_64-unknown-linux-gnu)

私は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",

似たようなものが実際に動作していても、それが私が望むものです。

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

ファイルの例は次のとおりです。

{
    "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,
    },
}

ベストアンサー1

正しい方法は次のようにすることです。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
    }
}

サンプル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
    }
}

そしてソリューション自体は次のとおりです。

jq -r 'input_filename + " tire_id: \(.tire_id) appID: \(.cdc.appID)"' file*.json

出力:

file1.json tire_id: 213275925375485 appID: 233262274090443
file2.json tire_id: 1305436516186552 appID: 1164562920689523

おすすめ記事