2017-01-11 3 views
0

Versuch, eine Protokolldatei mit dem folgenden Beispielinhalt zu tail -f.Beenden einer JSON-Protokolldatei mit printf-ähnlicher Ausgabe

{"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} 

Jedoch habe ich die Ausgabe des Schwanzes möchte nicht das tatsächliche JSON-String in jeder Zeile sein, aber ein printf und begrenzte Version davon.

Also vielleicht so etwas wie dieses

name: common | hostname: localhost | pid: 65187 | level: 30 | msg: iOT API listening at http://[::]:8080 | time: 2017-01-11T00:20:30.221Z 

Antwort

1

Hier ist ein Python-Skelett Sie als Utility verwenden können (durch die Kombination von Stapelüberlauf Antworten bekommen):

#!/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) 
2

try this:

tail json.txt | awk -F "," '{OFS="\t"; print $1,$2,$3,$4,$5,$6 }'

zur Kenntnis, dass die json strint enthalten kann "" in dem Schlüssel und Wert seiner Einzelteile. sicher, ich empfehle Ihnen, Python zu verwenden, um JSON in Formant wie CVS zu konvertieren, ist es einfacher.

2

Diese relativ einfach mit jq ist:

tail -f | jq -r '"name: \(.name) | hostname: \(.hostname) | pid: \(.pid) | level: \(.level) | msg: \(.msg) | time: \(.time)"' 
Verwandte Themen