2012-03-12 99 views
4

我的日志文件每行有一个json对象。 我使用[JSON] [1]到经由Combine tail -F和json

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

+0

'&|'?这是什么样的外壳? – 2012-03-12 13:40:11

+0

'zsh',但我认为它不会做我认为它... – osdf 2012-03-12 14:02:16

+0

你可能想看看https://bitbucket.org/bartwen/bsonify/ – 2014-03-01 13:27:57

回答

10

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

'echo“$ line”'。 – 2012-03-12 13:47:59

+0

@KarolyHorvath好点。固定。 – 2012-03-12 13:51:18

+1

我有一些带有反斜杠的json - class:“path \\ to \\ class”。为了保留这些,我必须将读取语句更改为读取-r行,从而保留它们 – dastra 2015-01-28 14:02:53