bash awk:将日期字符串从 YYYYMMDD 格式化为 YYYY-MM-DD
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22472385/
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
awk: format date string from YYYYMMDD to YYYY-MM-DD
提问by Manuel Weitzel
I have a CSV
file which I parse using awk
because I don't need all columns.
The problem I have is that one column is a date but in the format YYYYMMDD
but I need it in YYYY-MM-DD
and I don't know how to achieve that.
我有一个CSV
要解析的文件,awk
因为我不需要所有列。我遇到的问题是,一列是日期,但采用了格式,YYYYMMDD
但我需要它,但我YYYY-MM-DD
不知道如何实现。
I already tried with split($27, a)
but it doesn't split it - so a[0]
returns the whole string.
我已经尝试过,split($27, a)
但它没有拆分它 - 所以a[0]
返回整个字符串。
回答by devnull
回答by Khanjarrr
Use your awk
output as input to date -d
, e.g.
使用您的awk
输出作为输入date -d
,例如
$ date -d 20140918 +'%Y-%m-%d'
2014-09-18