Tail a file and strip datetime

Tail a file and strip datetime

Ich habe eine Protokolldatei, die den folgenden Inhalt hat.

2021-06-15T22:50:11+00:00 DEBUG {"slug": "something", "key2": "value2"}

Ich möchte tail -fdiese Datei und die Ergebnisse an jqden Befehl weiterleiten, muss aber 2021-06-15T22:50:11+00:00 DEBUGvor der Weiterleitung einen Teil entfernen, jqda jqeine JSON-Zeichenfolge erwartet wird.

Gibt es eine Möglichkeit, die Protokolldatei zu verfolgen und gleichzeitig den Datums-/Uhrzeitteil zu entfernen?

Letztendlich würde ich gerne folgenden Befehl verwenden.

tail -f :file | jq

Antwort1

Vorausgesetzt, Sie haben Zugriff auf GNU sed, das eine ungepufferte Ausgabe ermöglicht:

tail -f file | sed -u 's/^[^{]*//' | jq .

tail -fDies wird auf Ihrer Datei ausgeführt und sendet kontinuierlich neue Daten an sed. Der sedBefehl entfernt alles bis zum Leerzeichen vor dem ersten {in der Zeile und sendet das Ergebnis dann an jq.

The -u option to GNU sed makes it not buffer the output. Without this option, sed would buffer the result and would only send data to jq once the buffer (4 Kb?) was full. Doing buffering like this is standard procedure when the output of a tool is not the terminal itself, and it's done for efficiency reasons. In this case, we may want to turn the buffering off, so we use -u.

To select only lines that contain the DEBUG string before the JSON data:

tail -f file | sed -u -e '/^[^{]*DEBUG /!d' -e 's///' | jq .

or

tail -f file | sed -u -n 's/^[^{]*DEBUG //p' | jq .

The sed command here would delete all lines that do not start with some text not containing { characters, ending in DEBUG. If such a line is found, the matched text is removed, leaving the JSON data.

Note that we here extract the JSON based on the DEBUG string rather than the { that initiates a JSON object.

Related to buffering in pipelines:

Antwort2

To remove the first 2 space delimited columns:

tail -f file | stdbuf -oL cut -d ' ' -f3- | jq .

(stdbuf -oL, as found on GNU or FreeBSD systems, tricks cut into doing line-based instead of block-based output buffering).

verwandte Informationen