输入样本:
one
two 1578678600
awk命令:
$ echo -e "one\ntwo 1578678600"|awk '{print $1,strftime("%Y-%m-%d %H:%M:%S", $2)}'
one 1970-01-01 00:00:00
two 2020-01-10 17:50:00
如何strftime
忽略空字段而不打印“1970-01-01 00:00:00”?空字段预计在处理后保留为空。
答案1
我想到了
$ echo -e "one\ntwo 1578678600"|awk '{if($2!="") {t=strftime("%Y-%m-%d %H:%M:%S", $2)} else {t=$2}};{print $1,t}'
one
two 2020-01-10 17:50:00
甚至更好:
$ echo -e "one\ntwo 1578678600"|awk '{print $1,($2 != ""? strftime("%Y-%m-%d %H:%M:%S", $2) : $2)}'
one
two 2020-01-10 17:50:00