unix 文件日期从 %m/%d/%Y 转换

unix file date conversion from %m/%d/%Y

在 unix 服务器上,我有一个包含以下格式数据的文件:

4/1/2014,text1,text2,text3,text4
4/1/2014,....
...
...
4/20/2014,text1,text2,text3,text4
4/21/2014,...
...
...
10/14/2014,text1,text2,text3,text4
10/15/2014,...
...
...

这个完整的文件需要转换成以下格式:

2014-04-01,text1,text2,text3,text4
2014-04-01,....
...
...
2014-04-20,text1,text2,text3,text4
2014-04-21,...
...
...
2014-10-14,text1,text2,text3,text4
2014-10-15,...
...
...

请就此提出一些解决方案。

应该这样做:

awk -F, '{split(,a,"/");=a[3]"-"sprintf("%02d",a[1])"-"sprintf("%02d",a[2])}1' OFS=, file
2014-04-01,text1,text2,text3,text4
sed '\#^[0-9]\{1,2\}/[0-9]\{1,2\}/[0-9]\{4\}# {
        s#^\([^/]*\)/#0-0#;s#/#-#
        s/^0*\([0-9]\{2\}-\)0*\([0-9]\{2\}\)-\([0-9]\{4\}\)/-,/
        }' YourFile

只需重新格式化并在需要时处理额外的 0,并使用过滤器(如果所有行都有此过滤器则可选)以限制此开始日期)