regex - 在文件中替换/插入时间戳

标签 regex linux file replace sed

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

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

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": {“搜索查询”:“离合器s for women"},"data":{},"device_id":"ff71c4f0-81b8-4a44-8db1-1555e22f6761R","language":"English","device_model":"Micromax A310","country": "India","re​​gion":"Karnataka","adid":null,"session_id":1480550372776,"device_family":"Micromax Phone","idfa":null,"client_event_time":"2016-11-30 23 :59:53.406000"}

最佳答案

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

关于regex - 在文件中替换/插入时间戳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41973884/

相关文章:

sql-server - 无法为 CMS WebGUI 创建数据库链接

linux - 为什么重启后ntpd服务没有自动启动?

android - 从内容 URI 获取文件路径

正则表达式替换配置单元中的解决方案以屏蔽前 6 个字符

asp.net - URL 中的波浪线

python - 在python中提取嵌套正则表达式的所有匹配项

php - 获取 Linux 上 PHP 任务消耗的 CPU %

python - 在 Python 中读入文件并跳过文本文件的 header 部分

linux - 有人能告诉我如何用 1 字节长的缓冲区读取 10 字节吗?

php - 正则表达式 PHP,提取 id 并替换