结合尾部-F和json

时间:2012-03-12 13:13:09

标签: json tail

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

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

现在我想

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

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

mylog.log看起来像这样:

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

有什么建议吗?

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

1 个答案:

答案 0 :(得分:12)

它看起来像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