gpt4 book ai didi

bash - 在由 1 行组成的巨大 (12GB) 中用 }\n 替换每个 }?

转载 作者:行者123 更新时间:2023-11-29 09:02:12 25 4
gpt4 key购买 nike

我有一个日志文件(来自客户)。 18 场演出。该文件的所有内容都在 1 行中。我想读取 logstash 中的文件。但是由于内存,我遇到了问题。该文件逐行读取,但不幸的是它全部在 1 行上。

我尝试将文件分成几行以便 logstash 可以处理它(该文件具有简单的 json 格式,没有嵌套对象)我想将每个 json 放在一行中,在 处拆分替换为 }\n:

sed -i 's/}/}\n/g' NonPROD.log.backup

但是 sed 被杀死了——我想也是因为内存的原因。我该如何解决这个问题?我可以让 sed 使用行之外的其他数据 block 来操作文件吗?我知道默认情况下 sed 逐行读取。

最佳答案

以下仅使用 shell 中内置的功能:

#!/bin/bash

# as long as there exists another } in the file, read up to it...
while IFS= read -r -d '}' piece; do
# ...and print that content followed by '}' and a newline.
printf '%s}\n' "$piece"
done

# print any trailing content after the last }
[[ $piece ]] && printf '%s\n' "$piece"

如果您将 logstash 配置为从 TCP 端口读取(使用 14321 作为下面的任意示例),您可以运行 thescript <NonPROD.log.backup >"/dev/tcp/127.0.0.1/14321"或类似的,你就在那里 - 不需要在磁盘上有双倍的原始输入文件的可用空间,因为到目前为止给出的其他答案需要。

关于bash - 在由 1 行组成的巨大 (12GB) 中用 }\n 替换每个 }?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44851699/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com