我有以下格式的CSV文件
YYYY-mm-dd HH:MM:SS Some commentary text
YYYY-mm-dd HH:MM:SS Some commentary text
YYYY-mm-dd HH:MM:SS Some commentary text
YYYY-mm-dd HH:MM:SS Some commentary text
等。
如何为每个日期条目添加2小时?我知道日期将在每行的前19个字符中。
答案 0 :(得分:0)
快速而肮脏的解决方案:
while read line ; do OldDate="$(echo "$line"| awk '{print $1" "$2}')" ; NewDate=$(date "+%Y-%m-%d %H:%M:%S" -d "$OldDate +2 hours"); echo "$line" | sed "s/$OldDate/$NewDate/g" ; done < original.csv > modified.csv
答案 1 :(得分:0)
awk -v "DecalHour=2" '
{
OldString = substr( $0, 20)
OldTime = substr( $0, 1, 19)
gsub( /[^[:digit:]]/, " ", OldTime)
NewTime = mktime( OldTime ) + DecalHour * 60 * 60
NewStringTime = strftime( "%Y-%m-%d %H:%M:%S", NewTime)
# old - new
#print $0 " -> " NewStringTime OldString
# only new content
print NewStringTime OldString
}
' YourFile
注释:
答案 2 :(得分:0)
你也可以用Python做到这一点
"""input
2017-01-01 12:00:00,Some commentary text
2017-01-02 12:00:00,Some commentary text
2017-01-03 12:00:00,Some commentary text
2017-01-04 12:00:00,Some commentary text
"""
#said it was a csv, right :-)
import numpy as np
import pandas as pd
df=pd.read_csv("data.csv",names=[0,1])
df[0]=pd.to_datetime(df[0])+np.timedelta64(2,'h')
df.to_csv("/tmp/ans.csv",index=False,header=False)
"""
output looks like this:
2017-01-01 14:00:00,Some commentary text
2017-01-02 14:00:00,Some commentary text
2017-01-03 14:00:00,Some commentary text
2017-01-04 14:00:00,Some commentary text
"""