Skip to content

Commit d048b9b

Browse files
authored
Fix #8 and #9 (#11)
* Fix #8 and #9 * Updated README
1 parent 4cd4434 commit d048b9b

File tree

2 files changed

+25
-9
lines changed

2 files changed

+25
-9
lines changed

JSONPath.sh

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ main() {
2727
# ---------------------------------------------------------------------------
2828
# It all starts here
2929

30+
sanity_checks
3031
parse_options "$@"
3132

3233
trap cleanup EXIT
@@ -76,6 +77,19 @@ main() {
7677
fi
7778
}
7879

80+
# ---------------------------------------------------------------------------
81+
sanity_checks() {
82+
# ---------------------------------------------------------------------------
83+
84+
# Reset some vars
85+
for binary in gawk grep sed; do
86+
if ! which $binary >& /dev/null; then
87+
echo "$UNKNOWN: $binary binary not found in path. Aborting."
88+
exit 1
89+
fi
90+
done
91+
}
92+
7993
# ---------------------------------------------------------------------------
8094
reset() {
8195
# ---------------------------------------------------------------------------
@@ -362,7 +376,7 @@ create_filter() {
362376
else
363377
# Index in the range of 0-9 can be handled by regex
364378
query+="${comma}$(echo ${PATHTOKENS[i]} | \
365-
awk '/:/ { a=substr($0,0,index($0,":")-1);
379+
gawk '/:/ { a=substr($0,0,index($0,":")-1);
366380
b=substr($0,index($0,":")+1,index($0,"]")-index($0,":")-1);
367381
if(b>0) { print a ":" b-1 "]" };
368382
if(b<=0) { print a ":]" } }' | \
@@ -571,7 +585,7 @@ indexmatcher() {
571585
a=${q%:*} # <- number before ':'
572586
b=${q#*:} # <- number after ':'
573587
[[ -z $b ]] && b=99999999999
574-
readarray -t num < <( (grep -Eo ',[0-9]+[],]' | tr -d ,])<<<$line )
588+
readarray -t num < <( (grep -Eo '[0-9]+[],]' | tr -d ,])<<<$line )
575589
if [[ ${num[i]} -ge $a && ${num[i]} -lt $b && matched -eq 1 ]]; then
576590
matched=1
577591
[[ $i -eq $((${#INDEXMATCH_QUERY[*]}-1)) ]] && {
@@ -806,21 +820,21 @@ filter() {
806820
while read line; do
807821
v=${line#*$tab}
808822
case $OPERATOR in
809-
'-ge') if awk '{exit !($1>=$2)}'<<<"$v $RHS";then echo "$line"; fi
823+
'-ge') if gawk '{exit !($1>=$2)}'<<<"$v $RHS";then echo "$line"; fi
810824
;;
811-
'-gt') if awk '{exit !($1>$2) }'<<<"$v $RHS";then echo "$line"; fi
825+
'-gt') if gawk '{exit !($1>$2) }'<<<"$v $RHS";then echo "$line"; fi
812826
;;
813-
'-le') if awk '{exit !($1<=$2) }'<<<"$v $RHS";then echo "$line"; fi
827+
'-le') if gawk '{exit !($1<=$2) }'<<<"$v $RHS";then echo "$line"; fi
814828
;;
815-
'-lt') if awk '{exit !($1<$2) }'<<<"$v $RHS";then echo "$line"; fi
829+
'-lt') if gawk '{exit !($1<$2) }'<<<"$v $RHS";then echo "$line"; fi
816830
;;
817831
'>') v=${v#\"};v=${v%\"}
818832
RHS=${RHS#\"};RHS=${RHS%\"}
819-
[[ "$v" > "$RHS" ]] && echo "$line"
833+
[[ "${v,,}" > "${RHS,,}" ]] && echo "$line"
820834
;;
821835
'<') v=${v#\"};v=${v%\"}
822836
RHS=${RHS#\"};RHS=${RHS%\"}
823-
[[ "$v" < "$RHS" ]] && echo "$line"
837+
[[ "${v,,}" < "${RHS,,}" ]] && echo "$line"
824838
;;
825839
esac
826840
done #< <(egrep $opts "$FILTER")

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ pattern
3838
3939
## Requirements
4040

41-
JSONPath.sh is a Bash script that uses the standard GNU tools: *bash*, *cat*, *sed*, *awk*, *grep*, and *seq*.
41+
JSONPath.sh is a Bash script that uses the standard GNU tools: *bash*, *cat*,
42+
*sed*, *gawk*, *grep*, and *seq*. NOTICE that gawk, gnu awk, is required, not
43+
mawk or nawk, but gawk.
4244

4345
## Installation
4446

0 commit comments

Comments
 (0)