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

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

问题描述

我想创建一个脚本来将.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,

非常感谢!

推荐答案

您不提供csv文件的摘录,所以我使用这个:

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 "${line//;/ }" "+%s") ; done
2011/11/25;12:00:00;1322218800
2010/11/25;13:00:00;1290686400
2009/11/25;19:00:00;1259172000

EDIT :删除了不必要的变量。)

(EDIT: Removed an uneccessary variable.)

EDIT2 :改变了date命令,脚本实际工作。)

(EDIT2: Altered the date command so the script actually works.)

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

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