【发布时间】:2020-09-16 12:45:46
【问题描述】:
输入文件
xyz|name1|address1|19600221|M|country1|20200129|etc1
xyz|name2|address2|19610321|M|country2|20200118|etc1
xyz|name3|address3|19520217|M|country3||etc1
xyz|name4|address4|19611111|M|country4||etc1
预期输出
xyz|name1|address1|1960-02-21|M|country1|2020-01-29|etc1
xyz|name2|address2|1961-03-21|M|country2|2020-01-18|etc1
xyz|name3|address3|1952-02-17|M|country3||etc1
xyz|name4|address4|1961-11-11|M|country4||etc1
我使用的代码
awk -F"|" '{OFS="|";$4=strftime("%Y-%m-%d", $4); print$0}' input.txt
并将输出重定向到新文件并为第 7 列运行相同的内容,但结果与预期不符,我得到了以下结果
xyz|name1|address1|1960-02-21|M|country1|1970-08-22|etc1
xyz|name2|address2|1961-03-21|M|country2|1970-08-22|etc1
xyz|name3|address3|1952-02-17|M|country3|1970-01-01|etc1
xyz|name4|address4|1961-11-11|M|country4|1970-01-01|etc1
我不明白为什么第 7 列的输出不同?关于这里有什么问题的任何建议?
【问题讨论】:
-
1961-0-321?那是什么星球? -
行星错字!对不起
-
strftime预计自纪元以来的秒数。strftime("%Y-%m-%d",19610321)=1970-08-16 -
只是一般注意事项:只要您可以仅使用
string函数修改 awk 中的日期时间格式,就这样做,不要使用time函数。