时间戳时代与GAWK CSV文件 [英] Timestamp to Epoch in a CSV file with GAWK
问题描述
展望人类可读的时间戳来使用preparation GAWK加载到一个MySQL数据库的CSV文件中划时代/ Unix时间转换。
数据示例:
{空}; 2013年11月26日;文本和放大器;设备;位置; /文件/路径/要/;周二,11月26日12:17; 1; 1385845647
希望利用第6列,周二,11月26日12:17,并转换为划时代的时间进行存储。所有时间将在美国东部时间格式。我意识到AWK是这种情况的工具,但不能完全似乎结构命令。目前有:
猫FILE_IN.CSV | awk的'BEGIN {FS = OFS =;} {$ 6 =的strftime(%S)} {}打印
然而,这将返回:
{空}; 2013年11月26日;文本和放大器;设备;位置; /文件/路径/要/; 1385848848; 1; 1385845647
presumably,这意味着我打电话的现阶段时间(1385848848是在执行的时候当前时期),而不是问的strftime
将字符串转换;但我无法想象另一种方式来这样做。
什么是 GAWK
/ 的strftime
来转换现有时间戳划时代?正确的语法
编辑:这问题似乎松散的联系<一个href=\"http://stackoverflow.com/questions/3452339/how-do-i-use-output-from-awk-in-another-command\">How我在使用其他命令输出AWK?
$猫文件
{NULL}; 2013年11月26日;文本和放大器;设备;位置; /文件/路径/要/;周二,11月26日12:17; 1; 1385845647$ GAWK'BEGIN {FS = OFS =;} {GSUB(/ - /,,$ 2); $ 2 = mktime($ 20 0 0)} 1'文件
{NULL}; 1385445600;文本和放大器;设备;位置; /文件/路径/要/;周二,11月26日12:17; 1; 1385845647
下面是如何从一般的任何格式使用当前格式为例,有意见,看看一步转换过程步转换的日期秒时代以来:
$猫tst.awk
功能cvttime(T,A){
拆分(T,A,/ [,:] + /)
#2013周二,11月26日下午10时17分
#=&GT;
#A [1] =2013
#A [2] =星期二
#一个[3] =十一月
#一个[4] =26
#一个[5] =10
#一个[6] =17
#一个[7] =PM 如果((一个[7] ==PM)及及(一个[5]所述; 12)){
一个[5] + = 12
}
#=&GT;一个[5] =22 一个[3] = SUBSTR(一个[3],1,3)
#=&GT;一个[3] =十一月 匹配(JanFebMarAprMayJunJulAugSepOctNovDec,一个[3])
一个[3] =(RSTART + 2)/ 3
#=&GT;一个[3] = 11 返回(mktime(一个[1],一[3],一[4],一[5],一[6]0))
}开始 {
MDT =周二,11月26日下午10点17
秒= cvttime(2013,MDT)
DT =的strftime(%Y-%M-%D%H:%M:%S,秒)
打印MDT ORS\\ T-&gt;中ORS秒\\ t \\ T-&gt;中DT
}
$ AWK -f tst.awk
周二11月26日下午10时17分
- &GT; 1385525820
- &GT; 2013年11月26日22时17分00秒
我敢肯定,你可以修改当前的问题。
另外,如果你没有GAWK可以编写cvttime()函数(借款@人造卫星日期
命令字符串):
$猫tst2.awk
功能cvttime(T,CMD,秒){
CMD =日期-d \\T\\'+%S'
CMD |函数getline秒
关闭(CMD)
返回秒
}开始 {
MDT =周二,11月26日下午10点17
秒= cvttime(MDT)
DT =的strftime(%Y-%M-%D%H:%M:%S,秒)
打印MDT ORS\\ T-&gt;中ORS秒\\ t \\ T-&gt;中DT
}
$
$ AWK -f tst2.awk
周二11月26日下午10时17分
- &GT; 1385525820
- &GT; 2013年11月26日22时17分00秒
我离开srtftime()在那里只是为了表明秒是正确的 - 以日期替换
您认为合适的。
对于非GAWK的版本,你只需要弄清楚如何获得今年到输入月/日期/时间字符串的方式,日期
明白,如果该事宜中,以你 - 不应该很难
Looking to convert human readable timestamps to epoch/Unix time within a CSV file using GAWK in preparation for loading into a MySQL DB.
Data Example:
{null};2013-11-26;Text & Device;Location;/file/path/to/;Tuesday, November 26 12:17 PM;1;1385845647
Looking to take column 6, Tuesday, November 26 12:17 PM, and convert to epoch time for storage. All times shown will be in EST format. I realize AWK is the tool for this, but can't quite seem to structure the command. Currently have:
cat FILE_IN.CSV | awk 'BEGIN {FS=OFS=";"}{$6=strftime("%s")} {print}'
However this returns:
{null};2013-11-26;Text & Device;Location;/file/path/to/;1385848848;1;1385845647
Presumably, this means I'm calling the current epoch time (1385848848 was current epoch at time of execution) and not asking strftime
to convert the string; but I can't imagine another way to doing this.
What is the proper syntax for gawk
/strftime
to convert an existing timestamp to epoch?
Edit: This question seems loosely related to How do I use output from awk in another command?
$ cat file
{null};2013-11-26;Text & Device;Location;/file/path/to/;Tuesday, November 26 12:17 PM;1;1385845647
$ gawk 'BEGIN{FS=OFS=";"} {gsub(/-/," ",$2); $2=mktime($2" 0 0 0")}1' file
{null};1385445600;Text & Device;Location;/file/path/to/;Tuesday, November 26 12:17 PM;1;1385845647
Here's how to generally convert a date from any format to seconds since the epoch using your current format as an example and with comments to show the conversion process step by step:
$ cat tst.awk
function cvttime(t, a) {
split(t,a,/[,: ]+/)
# 2013 Tuesday, November 26 10:17 PM
# =>
# a[1] = "2013"
# a[2] = "Tuesday"
# a[3] = "November"
# a[4] = "26"
# a[5] = "10"
# a[6] = "17"
# a[7] = "PM"
if ( (a[7] == "PM") && (a[5] < 12) ) {
a[5] += 12
}
# => a[5] = "22"
a[3] = substr(a[3],1,3)
# => a[3] = "Nov"
match("JanFebMarAprMayJunJulAugSepOctNovDec",a[3])
a[3] = (RSTART+2)/3
# => a[3] = 11
return( mktime(a[1]" "a[3]" "a[4]" "a[5]" "a[6]" 0") )
}
BEGIN {
mdt ="Tuesday, November 26 10:17 PM"
secs = cvttime(2013" "mdt)
dt = strftime("%Y-%m-%d %H:%M:%S",secs)
print mdt ORS "\t-> " secs ORS "\t\t-> " dt
}
$ awk -f tst.awk
Tuesday, November 26 10:17 PM
-> 1385525820
-> 2013-11-26 22:17:00
I'm sure you can modify that for the current problem.
Also, if you don't have gawk you can write the cvttime() function as (borrowing @sputnik's date
command string):
$ cat tst2.awk
function cvttime(t, cmd,secs) {
cmd = "date -d \"" t "\" '+%s'"
cmd | getline secs
close(cmd)
return secs
}
BEGIN {
mdt ="Tuesday, November 26 10:17 PM"
secs = cvttime(mdt)
dt = strftime("%Y-%m-%d %H:%M:%S",secs)
print mdt ORS "\t-> " secs ORS "\t\t-> " dt
}
$
$ awk -f tst2.awk
Tuesday, November 26 10:17 PM
-> 1385525820
-> 2013-11-26 22:17:00
I left srtftime() in there just to show that the secs was correct - replace with date
as you see fit.
For the non-gawk version, you just need to figure out how to get the year into the input month/date/time string in a way that date
understands if that maters to you - shouldn't be hard.
这篇关于时间戳时代与GAWK CSV文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!