awk は 2 つの部分文字列を出力します。

awk は 2 つの部分文字列を出力します。

ここには次のような入力があります。

Nov  8 19:32:18 hostname api[-]: {"api_id":"5b4b0977cdfe813c0","app_type":"PRODUCTION","status_code":"200 OK","bytes_received":0,"response_body":"","request_method":"GET","client_id":"","http_user_agent":"Mozilla/5.0 (Windows NT; Windows NT 10.0; en-US) WindowsPowerShell/5.1.17134.228","billing":{"amount":0,"provider":"none","currency":"USD","model":"free","trial_period_days":0},"datetime":"2018-11-08T19:32:18.036Z","time_to_serve_request":31,"uri_path":"/abc/abc","request_protocol":"https","request_method":"POST","http_host":"localhost:700","request_uri":"/_bulk","http_x_forwarded_server":"locahost.com","content_type":"text/plain","http_x_global_transaction_id":"8f4202d3cf31","http_x_forwarded_host":"analytics.com","http_x_forwarded_for":"1.1.6.3","request_path":"/_bulk","http_organization":"admin","http_x_client_ip":"127.0.0.1","content_length":"2738"},"catalog_name":"test","immediate_client_ip":"1.1.6.3","debug":[],"rateLimit":{"rate-limit":{"limit":"-1","count":"-1"},"rate-limit-1":{"limit":"-1","count":"-1"},"rate-limit-2":{"limit":"-1","count":"-1"},"per-minute":{"limit":"-1","count":"-1"}},"api_version":"1.0.0","bytes_sent":217,"product_name":"N/A","plan_name":"N/A","tags":["_geoip_lookup_failure"],"catalog_id":"5ab7d1ebe4b0ad8c50045165","gateway_geoip":{},"app_name":"N/A","@timestamp":"2018-11-08T19:32:18.118Z","request_body":"","org_id":"5aaf1467e4b0ad8c500450f3","api_name":"productapi","resource_id":"abc","plan_id":"","query_string":[]}

この入力から2つの値を印刷したいと思いますstatus_codeapi_name

これ"status_code":"200 OK""api_name":"productapi"

シェルスクリプトを200 productapi 使用しているかのように印刷できますか?awk

答え1

sed私はできますか?努力する

$ sed  's/^.*status_code":"//; s/ .*api_name":"/ /; s/".*$//' file
200 productapi

答え2

AWKを使用:

awk -F '[ "]' '{gsub(/.*status_code":"|OK".*api_name":"/,""); print $1" "$2}' fileName

200 productapi

関連情報