我有一個包含以下元素的文本檔案,其中第一列是日期,第二列是價格:
2014-03-16 35
2014-03-17 78
2014-03-18 90
2014-03-20 22
2014-03-21 90
2014-03-22 38
2014-03-24 92
我想用缺少的日期完成檔案并添加 NA 的價格,例如:
2014-03-16 35
2014-03-17 78
2014-03-18 90
2014-03-19 不適用
2014-03-20 22
2014-03-21 90
2014-03-22 38
2014-03-23 不適用
2014-03-24 92
非常感謝您的幫助
uj5u.com熱心網友回復:
你可以使用這些代碼來做到這一點:
temp.txt 檔案包含所有沒有空行的資料。
#get the started timestamp
START=$(date -d `head -n1 temp.txt |awk '{print $1}'` %s)
#get the ended timestamp
END=$(date -d `tail -n1 temp.txt |awk '{print $1}'` %s)
#output what you want
cat temp.txt |awk -v start=$START -v end=$END '{total[$1]=$2}END{for (i =start; i<=end;i =86400) {_t = strftime("%Y-%m-%d", i);if (_t in total){print _t,total[_t]}else{print _t" NA"}}}'
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/408874.html
標籤:
下一篇:這里的檔案如何在shell中作業
