-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjqgrep
executable file
·92 lines (84 loc) · 2.29 KB
/
jqgrep
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/bin/bash
# Parse arguments:
FLAGS=""
if ! TEMP=$(getopt -n "$0" -o ckvimnpsx -- "$@"); then exit 1; fi
eval set -- "$TEMP"
while true; do
case "$1" in
-c) COMMANDS=1;;
-k) KEYS=1;;
-v) VALUES=1;;
-i|-m|-n|-p|-s|-x) FLAGS+=$(echo "$1" | sed 's/^-//');;
--) shift;break;;
*) echo "got star"; break;;
esac
shift
done
if [[ "$KEYS" == "" && "$VALUES" == "" ]]; then
KEYS=1
VALUES=1
fi
PATTERN="$1"
FILE="$2"
if [[ "$PATTERN" == "" ]]; then
echo "Usage: ${0##*/} [-c|-k|-v|-i|-m|-n|-p|-s|-x] pattern [json-files...]
This program will search json files for the given pattern as a key or
value and optionally print the jq command for retrieving it.
Options:
-c: Show matches as jq commands
-k: Only match keys
-v: Only match values
-i: Case insensitive search
-n: Ignore empty matches
-s: Single line mode (´^´ -> ´\\A´, ´\$´ -> ´\\Z´)
-m: Multi line mode (´.´ will match newlines)
-p: Both s and m modes are enabled
-x: Extended regex format (ignore whitespace and comments)"
exit 1
fi
if ! which jq > /dev/null 2>&1; then echo "The 'jq' command is not installed, aborting." >&2; exit 1; fi
shift
jq -r \
--arg pattern "$PATTERN" \
--arg commands "$COMMANDS" \
--arg keys "$KEYS" \
--arg values "$VALUES" \
--arg flags "$FLAGS" \
'
def jqpath:
def t: test("^[A-Za-z_][A-Za-z0-9_]*$");
reduce .[] as $x
("";
if ($x|type) == "string"
then . + ($x | if t then ".\(.)" else ".[" + tojson + "]" end)
else . + "[\($x)]"
end);
paths as $path | getpath($path) as $myval | select(
(
(
$values != ""
) and (
(
( getpath($path) | type ) == "string"
) or (
( getpath($path) | type ) == "number"
)
) and (
getpath($path) | tostring | test($pattern;$flags)
)
) or (
(
$keys != ""
) and (
"\($path[-1])" | test($pattern;$flags)
)
)
) | $path | . as $gp | jqpath
| (input_filename | sub( "^(<stdin>|/dev/fd/[0-9]*)$" ; "" )) as $input
| if $commands != ""
then
"jq \u0027\(.)\u0027\(if $input == "" then "" else " "+$input end) # \"\($myval)\""
else
"\(if $input == "" then "" else $input + ": " end)\(.) == \"\($myval)\""
end
' "$@"