forked from kepkin/dev-shell-essentials
-
Notifications
You must be signed in to change notification settings - Fork 0
/
json_log_beauty.sh
executable file
·61 lines (52 loc) · 1.28 KB
/
json_log_beauty.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
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/bash
# Usage info
function __json_log_beauty_show_help() {
cat << EOF
Usage: ${0##*/} [-hi] [-s regex pattern]
Pretty prints json from input stream. Each line must be a correct json string.
-h display this help and exit
-s process only those lines that match pattern. Otherwise print as it is
-i match pattern case-insensetive
Example:
cat file | json_log_beauty -i -s "error\|except"
EOF
}
function json_log_beauty() {
# Initialize our own variables:
output_file=""
match_string=""
ignore=0
OPTIND=1 # Reset is necessary if getopts was used previously in the script. It is a good idea to make this local in a function.
while getopts "his:" opt; do
case "$opt" in
h)
__json_log_beauty_show_help
return 0
;;
i) ignore=1
;;
s) match_string=$OPTARG
;;
'?')
__json_log_beauty_show_help >&2
return 1
;;
esac
done
shift "$((OPTIND-1))" # Shift off the options and optional --.
sed_params="q1"
if [ $ignore -eq 1 ]; then
sed_params="I$sed_params"
fi
while read -r line; do
if [ -z "$match_string" ]; then
echo $line | python -m json.tool;
continue
fi
if (echo $line | sed -n -e "/$match_string/$sed_params"); then
echo $line
else
echo $line | python -m json.tool;
fi
done
}