【问题标题】:Combine tail -F and json结合tail -F和json
【发布时间】:2012-03-12 13:13:08
【问题描述】:

我的日志文件每行有一个 json 对象。 我使用 [json][1] 通过

获得人类可读的输出
cat mylog.log | json -a field1 field2

现在我想拥有

tail -F mylog.log | json -a field1 field2

用于连续输出。但这似乎不是 工作,外壳只是挂起。如果我使用&| 避免缓冲问题,输出就像 我只运行cat

mylog.log 看起来像这样:

{"field1": entry1a, "field2": entry2a, "field3": entry3a}
{"field1": entry1b, "field2": entry2b, "field3": entry3b}

有什么建议吗?

[1]https://github.com/trentm/json

【问题讨论】:

标签: json tail


【解决方案1】:

它看起来像 json first loads the whole stdin into a buffer and only then processes the data,但您仍然应该能够通过为添加到日志文件的每一行调用它来实现流处理,如下所示:

tail -F mylog.log | while read line; do echo "$line" | json -a field1 field2; done

【讨论】:

  • 我有一些带有转义反斜杠的 json - class:"path\\to\\class"。为了保留这些,我不得不将 read 语句更改为 read -r 行,从而保留它们
猜你喜欢
  • 2015-08-03
  • 2017-05-10
  • 2011-01-03
  • 2015-12-26
  • 1970-01-01
  • 2010-11-02
  • 1970-01-01
  • 2014-07-12
  • 1970-01-01
相关资源
最近更新 更多