2016-08-01 10 views
1

我有以下形式的csv文件:劈裂CSV字符串列「日期時間」到「日期」,「時間」文件

$ head purchases.csv 
id,userID,itemID,price,platform,day 
1,9132,id_005,3600,2,2014-10-30 17:29:46 
2,67894,id_005,3000,1,2015-04-23 21:22:55 
3,272780,id_004,1000,1,2014-11-27 16:58:30 
4,302396,id_001,100,1,2014-12-11 08:35:07 

現在,我想改變CSV的最後一列。目前,它的格式爲day2014-10-30 17:29:46即日期和時間之間有空格。但我想這列分成兩列daytime使變更後的csv文件就變成了:

$ head purchases.csv 
id,userID,itemID,price,platform,day,time 
1,9132,id_005,3600,2,2014-10-30,17:29:46 
2,67894,id_005,3000,1,2015-04-23,21:22:55 
3,272780,id_004,1000,1,2014-11-27,16:58:30 

如何從終端做呢?

+0

考慮一下,如果你真的想這樣做。大部分時間感知的工具(比如Excel)都能夠很好地讀取日期時間。 – chthonicdaemon

回答

2

在$ 6使用split

$ awk -v OFS=\, -F\, 'NR==1{print $0,"time";next} {split($6,a," "); print $1,$2,$3,$4,$5,a[1],a[2]}' test.in 
id,userID,itemID,price,platform,day,time 
1,9132,id_005,3600,2,2014-10-30,17:29:46 
2,67894,id_005,3000,1,2015-04-23,21:22:55 
3,272780,id_004,1000,1,2014-11-27,16:58:30 
4,302396,id_001,100,1,2014-12-11,08:35:07 

或者你可以使用gsub,只是用逗號代替空間:

$ awk -v OFS=\, -F\, 'NR==1{print $0,"time";next} {gsub(/ /,",",$6); print $0}' test.in 
+0

做得很好;我建議更突出地介紹第二種解決方案,因爲它更容易推廣。還要注意''sub()'(而不是'gsub()')就足夠了,因爲你只替換了_single_空間。 – mklement0

1

James Brown's answer是有幫助的,但是硬編碼來修改,而列還假設它是最後

一些簡單的調整概括解決方案:

awk -v ndx=6 -F, 'NR==1 {sub(/$/, ",time", $ndx); print; next} sub(" ", ",", $ndx)' \ 
    purchases.csv