【问题标题】:Using 'sed' on a JSON file to find and replace在 JSON 文件上使用“sed”来查找和替换
【发布时间】:2016-05-12 01:06:09
【问题描述】:

我正在尝试使用 sed 将 json 文件中的 >\u00018øÏùÄ 替换为 null,该 json 文件被另一个组错误地转换。但是 sed 似乎什么也没做

sed -i 's|B\u001eøøïùÄ|null|g' 02_20060711_002_2.json

例如,如果 json 看起来像

{"my_log (PA1)": {"test_number_n": "3999999", "timestamp_log": "04:28:21.218", "trans_blah_i": "0", "record_test_type": {"sequence_nbr_u": "999"}, "filler_2_s": "", "dd": {"ty": {"ex_user_code": {"country_id_s": "", "user_id_s": "", "filed2": ""}, "whose": {"testing_code": {"city_id_s": "", "user_id_s": "", "fggstomer_s": ""}, "ex_client_s": "", "filler_2_s": ""}, "series": {"ticket_c": "0", "strike_test_i": "0", "can_n": "0", "country_c": "0", "instrument_group_c": "0", "expiration_date_n": "0", "modifier_c": "0"}, "exchange_info_s": "", "customer_info_s": "B\u001eøøïùÄ", "onu": "34", "ak": "0", "trading_code": {"city_id_s": "jk", "user_id_s": "O05", "ex_cur_s": "21  "}}, "timestamp_in_q": "03:28:21.217"}}}

【问题讨论】:

  • 那么写一个java应用来解析文档?我们有 10TB 的这些文档。
  • 另外我应该提到,我尝试这样做的原因是因为解析器在读取 json 文件时似乎失败了 DATA_READ ERROR: Error parsing JSON - Invalid UTF-8 start byte 0xf8 File
  • 是的,我正在使用 apache Drill 来查询文件。谢谢,很有帮助

标签: json regex unix sed


【解决方案1】:

您需要在 sed 命令中转义反斜杠。 试试这个:

sed -i 's|B\\u001eøøïùÄ|null|g' 02_20060711_002_2.json

在本地试了一下,好像还可以。

【讨论】:

  • 有趣的是,如果我在包含一条记录的文件上运行它,它可以正常工作。如果我对整个文件运行它,它似乎没有做任何事情。 sed 对大文件有问题吗?
  • 不要介意它与文件的编码方式有关。
猜你喜欢
  • 2014-12-04
  • 2017-01-07
  • 2010-12-20
  • 2012-02-14
  • 2011-12-19
  • 1970-01-01
  • 2020-08-05
  • 2017-09-12
  • 2021-10-06
相关资源
最近更新 更多