Skip to content

Commit 3fcea4f

Browse files
committed
Added support for other filter expression operators + tests
1 parent abfd2ac commit 3fcea4f

13 files changed

Lines changed: 202 additions & 24 deletions

JSONPath.sh

Lines changed: 71 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -41,18 +41,12 @@ main() {
4141
cat "$STDINFILE" | tokenize | parse | filter | indexmatcher >$PASSFILE
4242

4343
[[ $MULTIPASS -eq 1 ]] && {
44-
# replace filter with index sequence
44+
# replace filter expression with index sequence
4545
SET=$(sed -rn 's/.*,([0-9]+)[],].*/\1/p' $PASSFILE | tr '\n' ,)
4646
SET=${SET%,}
4747
QUERY=$(echo $QUERY | sed "s/?(@[^)]\+)/$SET/")
48-
[[ $DEBUG -eq 1 ]] && {
49-
echo "QUERY=$QUERY"
50-
}
51-
# Reset some vars
52-
declare -a INDEXMATCH_QUERY
53-
PATHTOKENS=
54-
FILTER=
55-
MULTIPASS=0
48+
[[ $DEBUG -eq 1 ]] && echo "QUERY=$QUERY"
49+
reset
5650
continue
5751
}
5852

@@ -81,6 +75,19 @@ main() {
8175
fi
8276
}
8377

78+
# ---------------------------------------------------------------------------
79+
reset() {
80+
# ---------------------------------------------------------------------------
81+
82+
# Reset some vars
83+
declare -a INDEXMATCH_QUERY
84+
PATHTOKENS=
85+
FILTER=
86+
OPERATOR=
87+
RHS=
88+
MULTIPASS=0
89+
}
90+
8491
# ---------------------------------------------------------------------------
8592
usage() {
8693
# ---------------------------------------------------------------------------
@@ -274,7 +281,7 @@ create_filter() {
274281
[[ -z $operator ]] && { operator="=="; rhs=; }
275282
if [[ $rhs == *'"'* || $rhs == *"'"* ]]; then
276283
case $operator in
277-
'=='|'=') operator=
284+
'=='|'=') OPERATOR=
278285
if [[ $elem == '?(@' ]]; then
279286
# To allow search on @.property such as:
280287
# $..book[?(@.title==".*Book 1.*")]
@@ -286,27 +293,42 @@ create_filter() {
286293
fi
287294
FILTER="$query"
288295
;;
289-
'>=') operator="-ge"
290-
;;
291-
'>') operator="-gt"
296+
'>='|'>') OPERATOR=">"
297+
RHS="$rhs"
298+
query+="$comma[0-9]+,\"$elem\"[],][[:space:]\"]*"
299+
FILTER="$query"
292300
;;
293-
'<=') operator="-le"
301+
'<='|'<') OPERATOR="<"
302+
RHS="$rhs"
303+
query+="$comma[0-9]+,\"$elem\"[],][[:space:]\"]*"
304+
FILTER="$query"
294305
;;
295-
'<') operator="-lt"
296306
esac
297307
else
298308
case $operator in
299-
'=='|'=') operator=
309+
'=='|'=') OPERATOR=
300310
query+="$comma[0-9]+,\"$elem\"[],][[:space:]\"]*$rhs"
301311
FILTER="$query"
302312
;;
303-
'>=') operator="-ge"
313+
'>=') OPERATOR="-ge"
314+
RHS="$rhs"
315+
query+="$comma[0-9]+,\"$elem\"[],][[:space:]\"]*"
316+
FILTER="$query"
304317
;;
305-
'>') operator="-gt"
318+
'>') OPERATOR="-gt"
319+
RHS="$rhs"
320+
query+="$comma[0-9]+,\"$elem\"[],][[:space:]\"]*"
321+
FILTER="$query"
306322
;;
307-
'<=') operator="-le"
323+
'<=') OPERATOR="-le"
324+
RHS="$rhs"
325+
query+="$comma[0-9]+,\"$elem\"[],][[:space:]\"]*"
326+
FILTER="$query"
308327
;;
309-
'<') operator="-lt"
328+
'<') OPERATOR="-lt"
329+
RHS="$rhs"
330+
query+="$comma[0-9]+,\"$elem\"[],][[:space:]\"]*"
331+
FILTER="$query"
310332
esac
311333
fi
312334
MULTIPASS=1
@@ -361,9 +383,7 @@ create_filter() {
361383
done
362384

363385
[[ -z $FILTER ]] && FILTER="$query[],]"
364-
[[ $DEBUG -eq 1 ]] && {
365-
echo "FILTER=$FILTER"
366-
}
386+
[[ $DEBUG -eq 1 ]] && echo "FILTER=$FILTER"
367387
}
368388

369389
# ---------------------------------------------------------------------------
@@ -763,9 +783,36 @@ filter() {
763783
# ---------------------------------------------------------------------------
764784
# Apply the query filter
765785

786+
local a tab=$(echo -e "\t") v
787+
766788
[[ $NOCASE -eq 1 ]] && opts+="-i"
767789
[[ $WHOLEWORD -eq 1 ]] && opts+=" -w"
768-
egrep $opts "$FILTER"
790+
if [[ -z $OPERATOR ]]; then
791+
egrep $opts "$FILTER"
792+
else
793+
egrep $opts "$FILTER" | \
794+
while read line; do
795+
v=${line#*$tab}
796+
case $OPERATOR in
797+
'-ge') if awk '{exit !($1>=$2)}'<<<"$v $RHS";then echo "$line"; fi
798+
;;
799+
'-gt') if awk '{exit !($1>$2) }'<<<"$v $RHS";then echo "$line"; fi
800+
;;
801+
'-le') if awk '{exit !($1<=$2) }'<<<"$v $RHS";then echo "$line"; fi
802+
;;
803+
'-lt') if awk '{exit !($1<$2) }'<<<"$v $RHS";then echo "$line"; fi
804+
;;
805+
'>') v=${v#\"};v=${v%\"}
806+
RHS=${RHS#\"};RHS=${RHS%\"}
807+
[[ "$v" > "$RHS" ]] && echo "$line"
808+
;;
809+
'<') v=${v#\"};v=${v%\"}
810+
RHS=${RHS#\"};RHS=${RHS%\"}
811+
[[ "$v" < "$RHS" ]] && echo "$line"
812+
;;
813+
esac
814+
done #< <(egrep $opts "$FILTER")
815+
fi
769816
}
770817

771818
# ---------------------------------------------------------------------------
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
$.store.book[?(@.price<4.2)].[title,price]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
$.store.book[?(@.price<=4.2)].[title,price]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
$.store.book[?(@.price>4.2)].[title,price]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
$.store.book[?(@.price>=4.2)].[title,price]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
$.store.book[?(@.title<"j")].title
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
$.store.book[?(@.title>"j")].title
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
["store","book",4,"title"] "Planet Urth"
2+
["store","book",4,"price"] 0
3+
["store","book",5,"title"] "Girl From Above: Betrayal (The 1000 Revolution)"
4+
["store","book",5,"price"] 2.53
5+
["store","book",6,"title"] "Girl From Above: Escape (The 1000 Revolution Book 2)"
6+
["store","book",6,"price"] 2.53
7+
["store","book",7,"title"] "Girl From Above: Trapped (The 1000 Revolution Book 3)"
8+
["store","book",7,"price"] 3.53
9+
["store","book",8,"title"] "Girl From Above: Trust (The 1000 Revolution Book 4)"
10+
["store","book",8,"price"] 3.53
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
["store","book",4,"title"] "Planet Urth"
2+
["store","book",4,"price"] 0
3+
["store","book",5,"title"] "Girl From Above: Betrayal (The 1000 Revolution)"
4+
["store","book",5,"price"] 2.53
5+
["store","book",6,"title"] "Girl From Above: Escape (The 1000 Revolution Book 2)"
6+
["store","book",6,"price"] 2.53
7+
["store","book",7,"title"] "Girl From Above: Trapped (The 1000 Revolution Book 3)"
8+
["store","book",7,"price"] 3.53
9+
["store","book",8,"title"] "Girl From Above: Trust (The 1000 Revolution Book 4)"
10+
["store","book",8,"price"] 3.53
11+
["store","book",9,"title"] "Leviathan Wakes: Book 1 of the Expanse"
12+
["store","book",9,"price"] 4.20
13+
["store","book",10,"title"] "Caliban's War: Book 2 of the Expanse"
14+
["store","book",10,"price"] 4.20
15+
["store","book",11,"title"] "Abaddon's Gate: Book 3 of the Expanse"
16+
["store","book",11,"price"] 4.20
17+
["store","book",13,"title"] "The Naked God (Nights Dawn Book 3)"
18+
["store","book",13,"price"] 4.20
19+
["store","book",14,"title"] "The Neutronium Alchemist (Nights Dawn Book 2)"
20+
["store","book",14,"price"] 4.20
21+
["store","book",15,"title"] "The Abyss Beyond Dreams (Chronicle of the Fallers Book 1)"
22+
["store","book",15,"price"] 4.20
23+
["store","book",17,"title"] "Dark Space: The Original Trilogy (Books 1-3) (Dark Space Trilogies)"
24+
["store","book",17,"price"] 4.20
25+
["store","book",18,"title"] "Dark Space (Book 4): Revenge"
26+
["store","book",18,"price"] 4.20
27+
["store","book",19,"title"] "Dark Space (Book 5): Avilon"
28+
["store","book",19,"price"] 4.20
29+
["store","book",20,"title"] "Dark Space (Book 6): Armageddon"
30+
["store","book",20,"price"] 4.20
31+
["store","book",21,"title"] "The Honour of the Knights (Battle for the Solar System, #1)"
32+
["store","book",21,"price"] 4.20
33+
["store","book",22,"title"] "The Third Side (Battle for the Solar System, #2)"
34+
["store","book",22,"price"] 4.20
35+
["store","book",23,"title"] "Split Second"
36+
["store","book",23,"price"] 4.20
37+
["store","book",24,"title"] "BrainWeb"
38+
["store","book",24,"price"] 4.20
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
["store","book",0,"title"] "Sayings of the Century"
2+
["store","book",0,"price"] 8.95
3+
["store","book",1,"title"] "Sword of Honour"
4+
["store","book",1,"price"] 12.99
5+
["store","book",2,"title"] "Moby Dick"
6+
["store","book",2,"price"] 8.99
7+
["store","book",3,"title"] "The Lord of the Rings"
8+
["store","book",3,"price"] 22.99
9+
["store","book",12,"title"] "The Reality Dysfunction (Nights Dawn Book 1)"
10+
["store","book",12,"price"] 8.20
11+
["store","book",16,"title"] "Night Without Stars (Chronicle of the Fallers Book 2)"
12+
["store","book",16,"price"] 9.50

0 commit comments

Comments
 (0)