コマンド出力をタイムスタンプ付きcsvに変換する

コマンド出力をタイムスタンプ付きcsvに変換する

端末コマンドを使用して速度接続をテストしたいです。

./speedtest-cli

次を返します。

Retrieving speedtest.net configuration...
Retrieving speedtest.net server list...
Testing from Moscow, Russia (77.50.8.74)...
Selecting best server based on latency...
Hosted by East Telecom Ltd (Mytishchi) [10.81 km]: 7.433 ms
Testing download speed........................................
Download: 38.06 Mbit/s
Testing upload speed..................................................
Upload: 23.52 Mbit/s

次のCSV行に変換したいと思います。

12-12-2016 12:01 ; 38.06 ; 23.52 ;
12-12-2016 12:11 ; 39.00 ; 21.12 ;
12-12-2016 12:21 ; 37.06 ; 25.00 ;

単純なgrep関数を使用してこれを達成しようとしています。

grep 'Upload:' test.txt | sed 's/^.*: //'  >> test_res.txt

ただし、これはファイルから速度番号を取得し、パラメータの1つだけを上書きします。必要な形式に正確な変換を作成する方法。私は初めてbashスクリプトに触れました。

ベストアンサー1

awk  -v date="$(date +%d-%m-%Y\ %H:%M)" -v OFS=';' '/Download:/ { d=$2; }
     /Upload:/ { print date, d, $2, ""; d="" }' speedtest

おすすめ記事