
따라서 Ookla에서 공식 속도 테스트 클라이언트를 실행하고 CSV 파일로 출력할 때 출력에 타임스탬프 필드가 없는 것을 발견했습니다. JSON은 그렇습니다. 그러나 저는 jq에 대해 특별히 잘 알지 못하며 JSON 출력을 CSV로 변환하는 것은 유용하지 않습니다.
출력을 가져와서 앞에 타임스탬프가 있는 파일로 파이프하는 방법이 있습니까?
이것은 JSON으로 제공된 출력입니다.
{"type":"result","timestamp":"2021-07-22T16:14:17Z","ping":{"jitter":0.035999999999999997,"latency":3.9399999999999999},"download":{"bandwidth":117078051,"bytes":884657048,"elapsed":7601},"upload":{"bandwidth":117029963,"bytes":467614102,"elapsed":4006},"packetLoss":0,"isp":"CenturyLink","interface":{"internalIp":"192.168.0.35","name":"eth0","macAddr":"E4:5F:01:2F:1D:39","isVpn":false,"externalIp":"71.214.44.165"},"server":{"id":10161,"name":"CenturyLink","location":"Orlando, FL","country":"United States","host":"orlando.speedtest.centurylink.net","port":8080,"ip":"205.171.98.14"},"result":{"id":"64657421-d008-4053-9832-2d1a9b01b649","url":"https://www.speedtest.net/result/c/64657421-d008-4053-9832-2d1a9b01b649"}}
이는 CSV(헤더 포함)의 출력입니다.
"server name","server id","latency","jitter","packet loss","download","upload","download bytes","upload bytes","share url"
"The Villages - The Villages, FL","25753","33.338","0.302","0","117318528","112406990","1488776432","1053747984","https://www.speedtest.net/result/c/8bbb92b8-880d-4021-b5e5-c90206862d18"
"CenturyLink - Orlando, FL","10161","4.013","0.399","0","76816660","112435444","1158108878","473391675","https://www.speedtest.net/result/c/17508892-6fc7-4616-84bb-810d314c50af"
"CenturyLink - Orlando, FL","10161","3.533","0.407","0","115293486","97552291","1002647576","574510787","https://www.speedtest.net/result/c/9913a846-1fbf-4d69-a1e9-27430914d397"
내가 하려는 일은 추가 처리가 가능하도록 JSON에서 출력되는 추가된 타임스탬프 데이터를 CSV 형식으로 가져오는 것입니다.
답변1
이 하나의 라이너는 원하는 것을 출력합니다.
<JSON> | jq -r '.timestamp, .server.name, .server.id, .pi ng.latency, .ping.jitter, .packetLoss, .download.bandwidth, .upload.bandwidth, .download.bytes, .upload.bytes, .result.url' | tr '\n' ',' | sed '$s/,$/\n/' | tee -a <CSV File>
샘플 JSON을 사용하여 다음과 같이 원하는 모든 요소를 추출했습니다 jq -r
.
.timestamp
.server.name
.server.id
.ping.latency
.ping.jitter
.packetLoss
.download.bandwidth
.upload.bandwidth
.download.bytes
.upload.bytes
.result.url
그런 다음 결과를 파이프로 연결하여 tr `\n' ','
쉼표로 구분된 목록을 만든 다음 tr
의 결과를 파이프로 연결하여 sed '$s/,$/\n/'
뒤따르는 쉼표를 제거했습니다.
결과는 다음과 같아야 합니다.
2021-07-22T16:14:17Z,CenturyLink,10161,3.94,0.036,0,117078051,117029963,884657048,467614102,https://www.speedtest.net/result/c/64657421-d008-4053-9832-2d1a9b01b649
답변2
csv 파일을 직접 실행하려는 경우, 제가 직접 조사한 후 수행한 작업은 다음과 같습니다.
#!/bin/sh
now=\"$(date +"%Y-%m-%d %H:%M:%S")\",
ookla -c http://www.speedtest.net/api/embed/vz0azjarf5enop8a/config -f csv | grep -v download | sed "s/^/$now/" >> /mnt/merlin/speedtest.csv
가장 적합한 형식으로 날짜를 저장하십시오. 또한 생성된 데이터와 일치하도록 따옴표와 쉼표를 추가했습니다. 나중에 이것을 사용하게 됩니다.
ookla 출력을 실행하여 grep
헤더 행을 제거한 다음 sed
행 시작 부분에 생성한 타임스탬프를 삽입하는 데 사용했습니다. 그런 다음 이것을 내 CSV에 추가합니다. 내 라우터에서 이것을 크론 작업으로 실행하고 있습니다. 도움이 되었기를 바랍니다.