bash脚本以日期和时间列转换为unix时间戳以.csv [英] Bash script to convert a date and time column to unix timestamp in .csv

查看:160
本文介绍了bash脚本以日期和时间列转换为unix时间戳以.csv的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个脚本来两列转换成一个.csv文件这是日期和时间为UNIX时间戳。所以我需要从每一行获取的日期和时间列,将其转换并包含时间戳最终将其插入到一个附加列。

I am trying to create a script to convert two columns in a .csv file which are date and time into unix timestamps. So i need to get the date and time column from each row, convert it and insert it into an additional column at the end containing the timestamp.

谁能帮助我?到目前为止,我已经发现了unix命令的任何给予时间和日期转换为unixstamp:

Could anyone help me? So far i have discovered the unix command to convert any give time and date to unixstamp:

date -d "2011/11/25 10:00:00" "+%s"
1322215200

我和bash脚本编程没有经验,任何人都可以让我开始?

I have no experience with bash scripting could anyone get me started?

我的行和列的例子:

Columns: Date, Time, 
Row 1: 25/10/2011, 10:54:36,
Row 2: 25/10/2011, 11:15:17,
Row 3: 26/10/2011, 01:04:39,

在此先感谢这么多!

Thanks so much in advance!

推荐答案

您不提供从您的CSV文件的exerpt,所以我用这一个:

You don't provide an exerpt from your csv-file, so I'm using this one:

[foo.csv]
2011/11/25;12:00:00
2010/11/25;13:00:00
2009/11/25;19:00:00

下面是为您解决问题的一种方法:

Here's one way to solve your problem:

$ cat foo.csv | while read line ; do echo $line\;$(date -d "$t" "+%s") ; done
2011/11/25;12:00:00;1322218800
2010/11/25;13:00:00;1290686400
2009/11/25;19:00:00;1259172000

修改:卸下了uneccessary变量)

(EDIT: Removed an uneccessary variable.)

这篇关于bash脚本以日期和时间列转换为unix时间戳以.csv的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆