2015年1月15日星期四

convert date string to epoch in awk

convert date string to epoch in awk

In a program I need to convert date string like 2015-01-15 09:23:19 to epoch.

Suppose I have a text file a.txt like this:

2015-01-15 09:23:19
2015-01-15 09:23:30
2015-01-15 09:24:21

At first , I use shell to do it.

awk script1

awk -F"\t" '{ 
    cmd="date -d \""$1"\" +%s";
    cmd|getline epoch;
    print $1;
    close(cmd)
}'  a.txt > epoch.txt

Then, I use the mktime funciton in awk to do it:

awk -F"\t" '{
    a=$(NF-1); 
    split(a, vec, " ")
    split(vec[1], ymd,"-") 
    split(vec[2],hms,":") 
    a=ymd[1]" "ymd[2]" "ymd[3]" "hms[1]" "hms[2]" "hms[3]
    print $3"\t"$4"\t"$13"\t"mktime(a) 
}'  a.txt > epoch.txt

I compared this two methods to a 5 million line file. The first method takes several hours to do it while the second takes only about 1 min! The difference is amazing.

没有评论:

发表评论