使用以下示例内容尝试tail -f
日志文件。
{"name":"common","hostname":"kgilbert-mac.corp.realpage.com","pid":65184,"level":30,"msg":"iOT API listening at http://[::]:8080","time":"2017-01-11T00:20:26.359Z","v":0}
{"name":"common","hostname":"kgilbert-mac.corp.realpage.com","pid":65185,"level":30,"msg":"iOT API listening at http://[::]:8080","time":"2017-01-11T00:20:28.942Z","v":0}
{"name":"common","hostname":"kgilbert-mac.corp.realpage.com","pid":65187,"level":30,"msg":"iOT API listening at http://[::]:8080","time":"2017-01-11T00:20:30.221Z","v":0}
但是我希望尾部的输出不是每一行上的实际JSON字符串,而是printf及其限制版本。
所以也许这样的事情
name: common | hostname: localhost | pid: 65187 | level: 30 | msg: iOT API listening at http://[::]:8080 | time: 2017-01-11T00:20:30.221Z
答案 0 :(得分:2)
试试这个:
tail json.txt | awk -F "," '{OFS="\t"; print $1,$2,$3,$4,$5,$6 }'
请注意,json strint可以在其项的键和值中包含“,”。 为了安全起见,我建议你使用python将json转换为像cvs一样的共振峰,它更容易。
答案 1 :(得分:2)
jq
:
tail -f | jq -r '"name: \(.name) | hostname: \(.hostname) | pid: \(.pid) | level: \(.level) | msg: \(.msg) | time: \(.time)"'
答案 2 :(得分:2)
这是一个可以用作实用程序的python框架(通过组合堆栈溢出答案得到):
#!/usr/bin/python
import time
import subprocess
import select
import json
from pprint import pprint
def processIt( string ):
j = json.loads( string )
for k in j:
print "%s : %s" % ( k, j[k] )
f = subprocess.Popen(['tail','-F',"foo"],stdout=subprocess.PIPE,stderr=subprocess.PIPE)
p = select.poll()
p.register(f.stdout)
while True:
if p.poll(1):
try:
processIt(f.stdout.readline())
except:
pass
time.sleep(1)