替换/插入文件中的时间戳

时间:2017-02-01 07:09:48

标签: regex linux file replace sed

我有一个JSON文件,其中包含以下格式的数据。我正在为德鲁伊编写索引任务来摄取数据。它因时间戳(event_time)而拒绝。

有没有办法将event_time值从 2016-11-30 23:59:53.654000 转换为 2016-11-30T23:59:53.654Z ?< / p>

JSON文件:

{"app":14232,"device_carrier":"airtel","$schema":10,"city":"Bellary","user_id":"234293","uuid":"1d5b5328-b729-11e2-a0e5-22340a56812f","event_time":"2016-11-30 23:59:53.654000","platform":"Android","os_version":"4.4.2","insider_id":3426766963,"processed_time":"2016-12-01 00:00:05.429064","user_creation_time":"2015-12-14 18:34:04.632000","version_name":"2.08","ip_address":"117.266.132.112","paying":"true","dma":null,"user_properties":{"[SauceFlyer] campaign":"N\/A","[SauceFlyer] media source":"Chemical"},"client_upload_time":"2016-12-01 00:00:03.982000","$insert_id":"e43b2a4d-072f-2322-bd74-6bdb0b2e1f63","event_type":"Search","library":"insider-android\/2.9.2","device_type":"Micromax Owen Nitro","device_manufacturer":"Micromax","start_version":"1.57","location_lng":null,"server_upload_time":"2016-12-01 00:00:04.230000","event_id":286,"location_lat":null,"os_name":"android","insider_event_type":null,"device_brand":"Micromax","groups":{},"event_properties":{"Search Query":"clutches for women"},"data":{},"device_id":"ff71c4f0-81b8-4a44-8db1-1555e22f6761R","language":"English","device_model":"Micromax A310","country":"India","region":"Karnataka","adid":null,"session_id":1480550372776,"device_family":"Micromax Phone","idfa":null,"client_event_time":"2016-11-30 23:59:53.406000"}

2 个答案:

答案 0 :(得分:2)

使用sed的另一种方法(因为年,月,日,小时,分钟和秒不仅仅是[0-9]位数):

$ sed 's/\([12][0-9]\{3\}-[01][0-9]-[0-3][0-9]\) \([0-2][0-9]\(:[0-5][0-9]\)\{2\}\.[0-9]\{3\}\)000/\1T\2Z/' <<< "2016-11-30 23:59:53.654000"
2016-11-30T23:59:53.654Z

答案 1 :(得分:1)

如果您的输入位于input.json,并且您希望使用sed执行此操作:

sed 's/\([0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\}\) \([0-9]\{2\}:[0-9]\{2\}:[0-9]\{2\}\.[0-9]\{3\}\)[0-9]\{3\}/\1T\2Z/g' input.json

(是的,我同意这很难看。) 但是,它会替换输入中的所有时间戳,假设这是您想要的。如果没有,你必须扩展正则表达式以匹配它之前的密钥。

等效的perl单行“以提高可读性”:

cat input.json | perl -p -e 's/(\d{4}-\d{2}-\d{2}) (\d{2}:\d{2}:\d{2}.\d{3})\d{3}/\1T\2Z/g'