使用AWK;系统日期:从CSV数据转换领域 [英] Using AWK; System, Date: Convert data field from csv
问题描述
您好,我有这样的一些CSV文件的:
Hello i have some csv files like that:
"N.º","Fecha Tiempo, GMT-03:00","Temp, °C (LGR S/N: 10466185, SEN S/N: 10466185, LBL: Temperatura)","Acoplador separado (LGR S/N: 10466185)","Acoplador adjunto (LGR S/N: 10466185)","Host conectado (LGR S/N: 10466185)","Parado (LGR S/N: 10466185)","Final de archivo (LGR S/N: 10466185)"
1,03/03/14 01:00:00 PM,25.477,Registrado,,,,
2,03/03/14 02:00:00 PM,24.508,,,,,
3,03/03/14 03:00:00 PM,26.891,,,,,
4,03/03/14 04:00:00 PM,25.525,,,,,
5,03/03/14 05:00:00 PM,27.358,,,,,
然后我想转换成两个字段的数据小时的第二场:日期,小时
Then i wanna convert the second field of data-hour in two fields: date, hour
我确定与分割日期和时间,但是当我尝试在AM-PM至数小时小时转换成24小时我失败了。
I'm ok with split date and hour, but when i try to convert hours in am-pm to hours in 24hrs i failed.
使用所有文件的命令:
awk -F"," '{print $2}' *.csv|awk '{print $1","$2" "$3}'
我抵达该命令,特别是:
I'm arriving to that command, in particular:
echo "11:04:44 PM" | awk -F, -v hora=$1 '{system("date --date=$hora +%T");print $hora}'
00:00:00
11:04:44 PM
问题是内部系统(日期变量...怎么一回事,因为它返回0或空。
The problem is the variable inside system(date... beacuse it returns 0 or empty.
接下来的问题是如何做thath。
而finnally如何插入文件中tath变化。
Then the question is about how to do thath. And finnally how to insert tath changes inside the file.
谢谢,非常感谢!
推荐答案
在我的机器(Mac OS)中,你需要的命令
On my machine (Mac OS), the command you need is
echo "11:22:33 AM" | awk '{split($1,a,":"); if($2=="PM") {a[1]=a[1]+12;} print a[1] ":" a[2] ":" a[3]}'
这手工做的时间分裂(而不是依赖于日期
这是一个有点平台而定),并增加了12〜如果是下午的时间。
This does the splitting of the time manually (rather than relying on date
which is a bit platform dependent) and adds 12 to the time if it's PM.
所以,整个事情就变成了:
So the whole thing becomes:
awk -F"," '{print $2}' *.csv | awk '{split($1,a,":"); if($2=="PM") {a[1]=a[1]+12;} print a[1] ":" a[2] ":" a[3]}'
虽然你真的想跳过文件中的第一行,因此
Although you really want to skip the first line in the file, so
awk -F"," 'if(NR>1){print $2}' *.csv | awk '{split($1,a,":"); if($2=="PM") {a[1]=a[1]+12;} print a[1] ":" a[2] ":" a[3]}'
这篇关于使用AWK;系统日期:从CSV数据转换领域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!