-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextract-ts.sh
executable file
·49 lines (39 loc) · 1.37 KB
/
extract-ts.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#!/usr/bin/sh
# Author: [email protected]
#
# Description:
# Formats the content of a JSON log file to be passed to Monpoly.
# Run this script without arguments for more information
#
# Dependencies:
# jq : JSON parser
# exit on error, undefined var, pipefail
set -euo pipefail
read -r -d '' HELP_TEXT <<-EOM || :
Usage: ./extract-ts.sh <TIMESTAMP_PATH>
Example: cat json.log | ./extract-ts.sh ".path.to.timestamp" | monpoly
Formats the content of a JSON log file to be passed to Monpoly.
The content of the log file must be passed via STDIN to this script,
while the formatted output is written to STDOUT.
Each input line must consist of a single JSON record string.
This script accepts a JSON-path pointing to the field containing the timestamp to extract.
This path is passed directly to jq and follows the same syntactic rules:
https://stedolan.github.io/jq/manual/#Basicfilters
Concrete example:
printf '{"ts": 3, "data": {}}\n{"ts": 4, "data": {}}' | ./extract-ts.sh ".ts"
results in:
@3 {"ts": 3, "data": {}}
@4 {"ts": 4, "data": {}}
EOM
if [ $# != 1 ] || [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
if [ $# != 1 ]; then
printf "Invalid number of arguments (expected 1, but %i provided)\n\n" "$#"
fi
echo "$HELP_TEXT"
exit 1
fi
json_path="$1"
while read line; do
ts=$(printf '%s\n' "$line" | jq "$json_path")
printf '@%s %s\n' "$ts" "$line"
done