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

use*_*211 5 bash logging sed logstash

我有一个日志文件(来自客户).18演出.该文件的所有内容都在一行.我想在logstash中读取该文件.但是因为记忆我得到了问题.文件是逐行读取的,但不幸的是它全部在1行.

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

sed -i 's/}/}\n/g' NonPROD.log.backup
Run Code Online (Sandbox Code Playgroud)

但是sed被杀了 - 我也认为也是因为记忆.我该如何解决这个问题?我可以sed使用其他数据块来操作文件而不是行吗?我知道默认情况下sed逐行读取.

Cha*_*ffy 6

以下仅使用内置于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"
Run Code Online (Sandbox Code Playgroud)

如果您已将logstash配置为从TCP端口读取(使用14321下面的任意示例),您可以运行thescript <NonPROD.log.backup >"/dev/tcp/127.0.0.1/14321"或类似,并且您可以 - 无需将原始输入文件的空间提高一倍,因为其他答案因此远远要求.