我有这个日志文件,我经常检查它,由于它的格式,打印出来后更容易阅读。我想这样做。
登录文件,如:
2019-07-04T09:53:04-07:00 some.package.placeholder.stderr {"log": "The content", "foo": "bar", "baz": "blah"}
2019-07-04T10:15:37-07:00 some.package.placeholder.stderr {"log": "I'm actually", "foo": "bar", "baz": "blah"}
2019-07-04T10:15:37-07:00 some.package.placeholder.stderr {"log": "Interested on", "foo": "bar", "baz": "blah"}
我想做类似的事情
tail -f myLogFile | grep [...?...] | jq '.log'
所以当尾随我得到:
The content
I'm actually
Interested on
甚至:
2019-07-04T09:53:04-07:00 The content
2019-07-04T10:15:37-07:00 I'm actually
2019-07-04T10:15:37-07:00 Interested on
最佳答案
使用 GNU grep -o
:
$ tail file | grep -o '{[^}]*}' | jq -r '.log'
The content
I'm actually
Interested on
使用任何 awk:
$ tail file | awk 'sub(/.*{/,"{")' | jq -r '.log'
The content
I'm actually
Interested on
$ tail file | awk '{d=$1} sub(/.*{/,""){$0="{\"date\": \""d"\", " $0} 1' | jq -r '.date + " " + .log'
2019-07-04T09:53:04-07:00 The content
2019-07-04T10:15:37-07:00 I'm actually
2019-07-04T10:15:37-07:00 Interested on
最后一个的工作原理是将输入中的日期字段合并到 json 中,这样 jq 就可以选择并使用日志字段打印它。
关于json - 通过tail格式化和 pretty-print 日志,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56892361/