[英]format date in file using awk
Content of the file is 该文件的内容是
Feb-01-2014 one two
Mar-02-2001 three four
I'd like to format the first field (the date) to %Y%m%d
format 我想将第一个字段(日期)格式化为%Y%m%d
格式
I'm trying to use a combination of awk and date
command, but somehow this is failing even though i got the feeling i'm almost there: 我正在尝试使用awk和date
命令的组合,但是即使我感觉到我快要出现了,但这还是以某种方式失败了:
cat infile | awk -F"\t" '{$1=system("date -d " $1 " +%Y%m%d");print $1"\t"$2"\t"$3}' > test
this prints out date's usage pages which makes me think that the date command is triggered properly, but there is something wrong with the argument, do you see the issue somewhere? 这会打印出日期的用法页面,这使我认为date命令已正确触发,但是参数有问题,您在某处看到此问题吗? i'm not that familiar with awk, 我对awk不太熟悉,
You don't need date
for this, its simply rearranging the date string: 您不需要date
,只需重新排列日期字符串即可:
$ awk 'BEGIN{FS=OFS="\t"} {
split($1,t,/-/)
$1 = sprintf("%s%02d%s", t[3], (match("JanFebMarAprMayJunJulAugSepOctNovDec",t[1])+2)/3, t[2])
}1' file
20140201 one two
20010302 three four
You can use: 您可以使用:
while read -r a _; do
date -d "$a" '+%Y%m%d'
done < file
20140201
20010302
system() returns the exit code of the command. system()返回命令的退出代码。
Instead: 代替:
cat infile | awk -F"\t" '{"date -d " $1 " +%Y%m%d" | getline d;print d"\t"$2"\t"$3}'
$ awk '{var=system("date -d "$1" +%Y%m%d | tr -d \"\\n\"");printf "%s\t%s\t%s\n", var, $2, $3}' file
201402010 one two
200103020 three four
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.