jaq improvements - json2tsv - JSON to TSV converter
(HTM) git clone git://git.codemadness.org/json2tsv
(DIR) Log
(DIR) Files
(DIR) Refs
(DIR) README
(DIR) LICENSE
---
(DIR) commit faec67c1670631fcd8eaa7bfb9ac1f8f639b0621
(DIR) parent 45d005ecfcbc9c6735e9ced2e9f9d31cd6cfe0ce
(HTM) Author: Hiltjo Posthuma <hiltjo@codemadness.org>
Date: Wed, 31 Aug 2022 22:07:59 +0200
jaq improvements
* simplify: make awk expressions mandatory and don't list the same format as
json2tsv. If it is wanted just use json2tsv directly.
* use $* (not the array $@) to assign to a string, noticed by shellcheck.
* simplify: option parsing, allow using "-" as the start of an expression
(except "-n").
* set an EXIT trap to really make sure the temporary file is removed.
* improve documentation and add more details.
Diffstat:
M jaq | 42 ++++++++++++-------------------
M jaq.1 | 43 ++++++++++++++++++++-----------
2 files changed, 44 insertions(+), 41 deletions(-)
---
(DIR) diff --git a/jaq b/jaq
@@ -1,31 +1,21 @@
#!/bin/sh
-args=$(getopt n $*)
-if [ $? -ne 0 ]; then
- echo "usage: $0 [-n] [awk expressions...]" >&2
- exit 1
-fi
nflag=""
-set -- $args
-while [ $# -ne 0 ]; do
- case "$1" in
- -n)
- nflag="-n"; shift;; # json2tsv -n: show indices for array types.
- --)
- shift; break;;
- esac
-done
+if test "$1" = "-n"; then
+ nflag="-n"
+ shift
+fi
-e="$@" # awk expressions
-if [ $# -gt 0 ]; then
- statusfile=$(mktemp)
- # simulate pipefail if JSON data is invalid.
- { json2tsv ${nflag} -r -F '\x1f' -R '\x1e'; echo $? >"${statusfile}"; } | \
- LC_ALL=C awk "BEGIN { FS = \"\x1f\"; RS = \"\x1e\"; }${e}"
- statuscode="$(cat "${statusfile}")$?"
- rm -f "${statusfile}"
- test "${statuscode}" = "00"
-else
- # show the nodes per line.
- json2tsv ${nflag}
+if [ $# -le 0 ]; then
+ echo "usage: $0 [-n] <awk expressions...>" >&2
+ exit 1
fi
+expr="$*"
+
+# simulate pipefail if JSON data is invalid.
+statusfile=$(mktemp)
+trap -- "rm -f \"${statusfile}\"" "EXIT"
+{ json2tsv ${nflag} -r -F '\x1f' -R '\x1e'; echo $? >"${statusfile}"; } | \
+ LC_ALL=C awk "BEGIN { FS = \"\x1f\"; RS = \"\x1e\" }${expr}"
+statuscode="$(cat "${statusfile}" 2>/dev/null)$?"
+test "${statuscode}" = "00"
(DIR) diff --git a/jaq.1 b/jaq.1
@@ -1,36 +1,49 @@
-.Dd August 30, 2022
+.Dd August 31, 2022
.Dt JAQ 1
.Os
.Sh NAME
.Nm jaq
-.Nd json2tsv awk query, a json2tsv convenience wrapper script
+.Nd json2tsv awk query, a json2tsv convenience wrapper shellscript
.Sh SYNOPSIS
.Nm
.Op Fl n
-.Op Ar awk expressions...
+.Ar awk expressions...
.Sh DESCRIPTION
.Nm
reads JSON data from stdin.
-When any
-.Ar awk expressions
-are given then the
+The parsed nodes in the JSON data are then passed to
+.Xr awk 1
+in the format:
+.Bd -literal
+nodename<FIELD SEPARATOR>type<FIELD SEPARATOR>value<RECORD SEPARATOR>
+.Ed
+.Pp
+See the OUTPUT format section of
.Xr json2tsv 1
-options
+for a more detailed explanation.
+.Pp
+The options are as follows:
+.Bl -tag -width Ds
+.It Fl n
+Show the indices for array types (by default off).
+.El
+.Pp
+The options
.Fl r
and
.Fl F Ar '\ex1f'
and
.Fl R Ar '\ex1e'
-are passed also.
-If there are no
-.Ar awk expressions
-given then it passes the input to
+are passed to
.Xr json2tsv 1 .
-.Pp
-The options are as follows:
+.Sh ENVIRONMENT VARIABLES
.Bl -tag -width Ds
-.It Fl n
-Show the indices for array types (by default off).
+.It Ev LC_ALL
+This variable is set to the "C"
+.Xr locale 1
+for the
+.Xr awk 1
+program.
.El
.Sh EXIT STATUS
.Ex -std