在Linux中使用AWK将日期转换为纪元时间 [英] Convert date to epoch time using AWK in linux
问题描述
我有一个逗号分隔的文件,第一列的日期为01/31/2010 我想更改为时代,例如文件"file.csv":
I have a comma-separated file, with the first column as a date of the format 01/31/2010 that I want to change into epoch time, such that the file "file.csv":
01/30/2010,1,"hi"
01/31/2010,3,"bye"
将更改为"output.csv":
will change into "output.csv":
1264809600,1,"hi"
1264896000,3,"bye"
我知道命令行日期-d"01/30/2010" +%s可以使用,但是只能在一个日期上使用,我需要将其输入到表中,因此,有没有办法使用awk带有一些func():
I know the command line date -d "01/30/2010" +%s will work, but only on a single date, and I need to feed it into a table, so, is there a way to use awk with some func():
cat file.csv | awk -F, 'print func($1)","$2","$3}'
由于我不太在意如何执行此操作,因此,当字符串为mm/dd/yyyy ...时,如何将Excel中的日期更改为纪元...
Since I don't really care how I do this, alternatively, how would I change a date in excel into epoch, when the string is mm/dd/yyyy...
推荐答案
TZ=PST awk -F, '{split($1,date,"/");
$1=mktime(date[3] " " date[1] " " date[2] " " "00 00 00");
print}'
或者,调用date
:
TZ=PST awk -F, '{ OFS = FS;
command="date -d" $1 " +%s";
command | getline $1;
close(command);
print}'
这篇关于在Linux中使用AWK将日期转换为纪元时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!