这里我有这样的输入
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":[]}
我想从此输入打印两个值,即status_code
和api_name
在这个"status_code":"200 OK"
和 "api_name":"productapi"
我们可以像200 productapi
使用awk
shell 脚本一样打印吗?
答案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