我有以下格式的歷史自動生成的日誌文件,我想轉換爲csv文件上傳到數據庫之前在awk/Perl的轉換文本文件與感知的形式
--------------------------------------
Thu Jul 8 09:34:12 BST 2010
BLUE Head 1
Duration = 20 s
Activity = 14.9 MBq
Sensitivity = 312 cps/MBq
--------------------------------------
Thu Jul 8 09:34:55 BST 2010
BLUE Head 1
Duration = 20 s
Activity = 14.9 MBq
Sensitivity = 318 cps/MBq
--------------------------------------
Thu Jul 8 10:13:39 BST 2010
RED Head 1
Duration = 20 s
Activity = 14.9 MBq
Sensitivity = 307 cps/MBq
--------------------------------------
Thu Jul 8 10:14:10 BST 2010
RED Head 1
Duration = 20 s
Activity = 14.9 MBq
Sensitivity = 305 cps/MBq
--------------------------------------
Mon Jul 19 10:11:18 BST 2010
BLUE Head 1
Duration = 20 s
Activity = 12.4 MBq
Sensitivity = 326 cps/MBq
--------------------------------------
Mon Jul 19 10:12:09 BST 2010
BLUE Head 1
Duration = 20 s
Activity = 12.4 MBq
Sensitivity = 333 cps/MBq
--------------------------------------
Mon Jul 19 10:13:57 BST 2010
RED Head 1
Duration = 20 s
Activity = 12.4 MBq
Sensitivity = 338 cps/MBq
--------------------------------------
Mon Jul 19 10:14:45 BST 2010
RED Head 1
Duration = 20 s
Activity = 12.4 MBq
Sensitivity = 340 cps/MBq
--------------------------------------
我想到csv日誌文件轉換爲以下格式
Date,Camera,Head,Duration,Activity
08/07/10,BLUE,1,20,14.9
08/07/10,BLUE,1,20,14.9
08/07/10,RED,1,20,14.9
08/07/10,RED,1,20,14.9
我用awk來讓我接近我希望
awk 'BEGIN {print "Date,Camera,Head,Duration,Activity";RS = "--------------------------------------"; FS="\n";}; {OFS=",";split($3, a, " ");split($4,b, " "); split($5,c," ");print $2,a[1],a[3],b[3],c[3]}' sensitivity.txt > sensitivity.csv
這給了我
Date,Camera,Head,Duration,Activity
,,,,
Thu Jul 8 09:34:12 BST 2010,BLUE,1,20,14.9
Thu Jul 8 09:34:55 BST 2010,BLUE,1,20,14.9
Thu Jul 8 10:13:39 BST 2010,RED,1,20,14.9
Thu Jul 8 10:14:10 BST 2010,RED,1,20,14.9
我怎樣才能
(一)線擺脫了4場輸出的隔板4 (b)由週四7月8日9時34分12秒轉換日期格式BST 2010 DD/MM/YY(我能做到這一點的純AWK或通過管道到perl)
對於日期的轉換,看看第一個答案在這裏http://stackoverflow.com/questions/2121896/converting-dates-in-awk – marmottus 2013-04-10 10:30:28
而對於無用的逗號,只是檢查$ 2的值, a,b,c等...(if($ 2){print ...}) – marmottus 2013-04-10 10:34:19
我不認爲我曾見過任何人要求將自2000年以來的4位數年份轉換爲2位數年份一起滾。認真考慮使用YYYYMMDD日期格式,以便您可以區分1999年和2099年,並按日期對數據進行輕鬆排序。 – 2013-04-10 12:46:09