├── demo.png ├── dev-shell-essentials.sh ├── highlight.sh ├── test.log ├── README.md ├── LICENSE ├── json_log_beauty.sh └── json_log_beauty_h.py /demo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kepkin/dev-shell-essentials/HEAD/demo.png -------------------------------------------------------------------------------- /dev-shell-essentials.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/bash 2 | 3 | SCRIPT_PATH=$( dirname "${BASH_SOURCE[0]}" ) 4 | 5 | source $SCRIPT_PATH/highlight.sh 6 | source $SCRIPT_PATH/json_log_beauty.sh 7 | -------------------------------------------------------------------------------- /highlight.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/bash 2 | 3 | function highlight() { 4 | declare -A fg_color_map 5 | fg_color_map[black]=30 6 | fg_color_map[red]=31 7 | fg_color_map[green]=32 8 | fg_color_map[yellow]=33 9 | fg_color_map[blue]=34 10 | fg_color_map[magenta]=35 11 | fg_color_map[cyan]=36 12 | 13 | fg_c=$(echo -e "\e[1;${fg_color_map[$1]}m") 14 | c_rs=$'\e[0m' 15 | sed -u s"/$2/$fg_c\0$c_rs/g" 16 | } 17 | -------------------------------------------------------------------------------- /test.log: -------------------------------------------------------------------------------- 1 | {"@timestamp":"2014-08-18T10:27:25.604+0400","channel":"synchronization","message_type":"input","client":"C2362567-C4D1-434A-987C-CF326F3800C0"} 2 | {"@timestamp":"2014-08-18T10:30:25.604+0400","channel":"synchronization","message_type":"output","client":"C2362567-C4D1-434A-987C-CF326F3800C0"} 3 | {"@timestamp":"2014-08-18T10:32:25.604+0400","channel":"synchronization","message_type":"input","client":"0E3BFC78-3387-4C5B-A9AD-CDFD1FEA02D8"} 4 | 5 | {"@timestamp":"2014-08-18T10:40:25.604+0400","channel":"synchronization","message_type":"input","client":"0E3BFC78-3387-4C5B-A9AD-CDFD1FEA02D8"} 6 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | dev-shell-essentials 2 | ==================== 3 | 4 | ![Alt demo](https://github.com/kepkin/dev-shell-essentials/blob/master/demo.png "Example") 5 | 6 | ## Requirements 7 | 8 | * python with json module (almost all linux distro has it preinstalled) 9 | 10 | ## Installation 11 | 12 | ```bash 13 | wget https://github.com/kepkin/dev-shell-essentials/archive/master.zip -O dev-shell-essentials.zip && 14 | unzip dev-shell-essentials.zip && 15 | source dev-shell-essentials-master/dev-shell-essentials.sh 16 | 17 | ``` 18 | 19 | Or using git 20 | 21 | ```bash 22 | git clone https://github.com/kepkin/dev-shell-essentials.git 23 | cd dev-shell-essentials 24 | source dev-shell-essentials.sh 25 | 26 | ``` 27 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2014, kepkin 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without 5 | modification, are permitted provided that the following conditions are met: 6 | 7 | * Redistributions of source code must retain the above copyright notice, this 8 | list of conditions and the following disclaimer. 9 | 10 | * Redistributions in binary form must reproduce the above copyright notice, 11 | this list of conditions and the following disclaimer in the documentation 12 | and/or other materials provided with the distribution. 13 | 14 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 15 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 17 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 18 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 20 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 21 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 22 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 23 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 | 25 | -------------------------------------------------------------------------------- /json_log_beauty.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/bash 2 | 3 | # Usage info 4 | function __json_log_beauty_show_help() { 5 | cat << EOF 6 | Usage: ${0##*/} [-hi] [-s regex pattern] 7 | Pretty prints json from input stream. Each line must be a correct json string. 8 | 9 | -h display this help and exit 10 | -s process only those lines that match pattern. Otherwise print as it is 11 | -i match pattern case-insensetive 12 | 13 | Example: 14 | cat file | json_log_beauty -i -s "error\|except" 15 | 16 | EOF 17 | } 18 | 19 | function json_log_beauty() { 20 | # Initialize our own variables: 21 | output_file="" 22 | match_string="" 23 | ignore=0 24 | 25 | 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. 26 | while getopts "his:" opt; do 27 | case "$opt" in 28 | h) 29 | __json_log_beauty_show_help 30 | return 0 31 | ;; 32 | i) ignore=1 33 | ;; 34 | s) match_string=$OPTARG 35 | ;; 36 | '?') 37 | __json_log_beauty_show_help >&2 38 | return 1 39 | ;; 40 | esac 41 | done 42 | shift "$((OPTIND-1))" # Shift off the options and optional --. 43 | 44 | sed_params="q1" 45 | if [ $ignore -eq 1 ]; then 46 | sed_params="I$sed_params" 47 | fi 48 | 49 | while read -r line; do 50 | if [ -z "$match_string" ]; then 51 | echo $line | python -m json.tool; 52 | continue 53 | fi 54 | 55 | if (echo $line | sed -n -e "/$match_string/$sed_params"); then 56 | echo $line 57 | else 58 | echo $line | python -m json.tool; 59 | fi 60 | done 61 | } 62 | -------------------------------------------------------------------------------- /json_log_beauty_h.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | __author__ = 'Alexander.Nevskiy' 4 | 5 | import argparse 6 | import json 7 | import sys 8 | 9 | # todo: write unit test 10 | # data = {} 11 | # data['a'] = {} 12 | # data['a']['b'] = 3 13 | # data['a']['c'] = 45 14 | # data['a']['d'] = 4 15 | # 16 | # r = {} 17 | # set_value("a.b", data, r) 18 | # set_value("a.c", data, r) 19 | # print(r) 20 | 21 | def set_value(k, data, result): 22 | keys = k.split(".") 23 | 24 | a = result 25 | d = data 26 | for nk in keys[:-1]: 27 | if nk not in a: 28 | a[nk] = {} 29 | a = a[nk] 30 | d = data[nk] 31 | 32 | a[keys[-1]] = d[keys[-1]] 33 | 34 | 35 | def main(cmd): 36 | infile = sys.stdin 37 | outfile = sys.stdout 38 | indent = 4 39 | if cmd.collapse: 40 | indent = None 41 | 42 | for l in infile.readlines(): 43 | try: 44 | obj = json.loads(l) 45 | if cmd.show is not None: 46 | obj2 = {} 47 | for k in cmd.show: 48 | set_value(k, obj, obj2) 49 | obj = obj2 50 | json.dump(obj, outfile, sort_keys=True, indent=indent) 51 | print('') 52 | except ValueError as e: 53 | print("Failed decode json for: {}\n error: {}".format(l, e)) 54 | 55 | 56 | if __name__ == '__main__': 57 | parser = argparse.ArgumentParser() 58 | parser.add_argument('--show', required=False, type=str, nargs='+', 59 | help='What fields to show. You can use complex keys like Object.SubObject.field') 60 | parser.add_argument('-c', '--collapse', action='store_true', required=False, default=False, 61 | help='Useful when you don\'t want to prettify but get subset of json with --show') 62 | 63 | cmd = parser.parse_args() 64 | main(cmd) 65 | --------------------------------------------------------------------------------