如何转换文件中的所有unix日期?
我一直在寻找,但还没有成功完成这项任务..
I've been searching all over and haven't succeeded yet with this task..
我有一些带有多个日期的 gps 数据文件,以秒为单位显示为 Unix 纪元时间,我需要转换这些文件,以便将文件数据插入电子表格.
I have some gps data files with multiple dates presented as Unix epoch time in seconds and I need to convert these so that I can insert the file data into a spreadsheet.
date
可以转换它们,例如
$ date -d @1441202338 +'%d-%m-%y %H:%M:%S'
02-09-15 21:58:58
unix 日期是用引号括起来的十位数字,还有其他更长的数字,例如经度和纬度,因此为了仅替换日期,我需要在引号中找到十位数字.
The unix dates are ten digits enclosed with quotes, and there are other longer numbers such as longitude and latitude, so to replace only the dates I need to find exactly ten digits in quotes.
我已经开始构建一个 sed 命令,但我不知道如何在引号中搜索字符串,然后只将字符串发送到 date
I've started building a sed command, but I can't find out how to search for a string in quotes and then send only the string to date
sed "s/\([0-9]{10}\)/$(date -d @\1 +'%d-%m-%y %H:%M:%S')/g" file
是否将 \<\"
放在那里表示开头的引号?
would putting \<\"
in there denote a quote at the start?
我也不确定date
是否能理解\1
有人可以指导我解决问题吗?
Can someone guide me to a solution?
这是我尝试转换的文件中的示例行:
Here's a sample line from a file I'm trying to convert:
type="waypoint" latitude="22.2091608952380115" longitude="44.65250015586757" name="name" altitude="309.18737800000002" unixtime="1441202338"
其中 unixtime="1441202338"
必须更改为 unixtime="02-09-15 21:58:58"
.
where unixtime="1441202338"
must be changed to unixtime="02-09-15 21:58:58"
.
(很明显,日期将不再是unixtime",但该标签可以在之后更改)
(so obviously, the date will not be "unixtime" any more, but that tag can be changed afterwards)
一个最小的文件示例:
'#VIKING GPS Data file http://viking.sf.net/
FILE_VERSION=1
xmpp=4.000000
ympp=4.000000
lat=40.714490
lon=-74.007130
mode=mercator
color=#cccccc
highlightcolor=#eea500
drawscale=t
drawcentermark=t
drawhighlight=t
~Layer TrackWaypoint
name=name
tracks_visible=t
waypoints_visible=t
routes_visible=t
trackdrawlabels=t
trackfontsize=3
drawmode=0
trackcolor=#000000
drawlines=t
line_thickness=1
drawdirections=f
trkdirectionsize=5
drawpoints=t
trkpointsize=2
drawelevation=f
elevation_factor=30
drawstops=f
stop_length=60
bg_line_thickness=0
trackbgcolor=#ffffff
speed_factor=30.000000
tracksortorder=0
drawlabels=t
wpfontsize=3
wpcolor=#000000
wptextcolor=#ffffff
wpbgcolor=#8383c4
wpbgand=f
wpsymbol=0
wpsize=4
wpsyms=t
wpsortorder=1
drawimages=t
image_size=64
image_alpha=255
image_cache_size=300
metadatadesc=
metadataauthor=
metadatatime=2015-10-03T08:11:32.776627Z
metadatakeywords=
~LayerData
type="waypointlist"
type="waypoint" latitude="5.2091608952380115" longitude="101.65250015586757" name="different-names" altitude="309.18737800000002" unixtime="1441202338" symbol="navaid, amber"
type="waypointlistend"
~EndLayerData
~EndLayer'
使用 gawk
时间函数.
示例
echo 'altitude="309.18737800000002" unixtime="1441202338"'|\
gawk '$(NF-1)=strftime("%d-%m-%y %H:%M:%S",$(NF-1))' FS=\" OFS=\"
结果
altitude="309.18737800000002" unixtime="02-09-15 15:58:58"
完整文件要过滤输入文件,请使用:
gawk '/unixtime=/{$12=strftime("%d-%m-%y %H:%M:%S",$12)}1' FS=\" OFS=\" inputfile > targetfile
结果
...
...
...
~LayerData
type="waypointlist"
type="waypoint" latitude="5.2091608952380115" longitude="101.65250015586757" name="different-names" altitude="309.18737800000002" unixtime="02-09-15 15:58:58" comment="29-08-11 8:27:49" symbol="navaid, amber"
type="waypointlistend"
~EndLayerData
~EndLayer
如果您的 gawk
支持 inplace 替换,请检查:https://www.gnu.org/software/gawk/manual/html_node/Extension-Sample-Inplace.html
If your gawk
supports inplace replacement, check : https://www.gnu.org/software/gawk/manual/html_node/Extension-Sample-Inplace.html