bash 如何在awk脚本中将一列转换为日期格式?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15906557/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to convert one column to date format in awk script?
提问by user710818
cat test.txt |
awk 'BEGIN { FS = "\t" } ;{ print =strftime("%d %b %Y",) "\t" }'
I have received:
我收到了:
31 12 1969 2012-09-27
But I have expected:
但我已经预料到:
27 Sep 2012 2012-09-27
Where is the error? Thanks.
错误在哪里?谢谢。
回答by Kent
if you really want to convert the format with awk build-in functions, this example may help you:
如果你真的想用 awk 内置函数转换格式,这个例子可以帮助你:
kent$ echo "2012-09-27" |awk '{t= mktime(datespec)
strftime([format [, timestamp[, utc-flag]]])
;gsub("-"," ",t);print strftime("%d %b %Y", mktime(t" 00 00 00"))}'
27 Sep 2012
read the description of following functions in man page:
阅读手册页中对以下功能的描述:
awk 'BEGIN {FS=OFS="\t"}{t=;gsub("-"," ",t); print strftime("%d %b %Y", mktime(t" 00 00 00")),}' test.txt
edittry to fix you old command, but no input file, cannot test it...
编辑尝试修复您的旧命令,但没有输入文件,无法对其进行测试...
##代码##
