Introduction
Introduction Statistics Contact Development Disclaimer Help
add jtq: a convenience wrapper script - json2tsv - JSON to TSV converter
git clone git://git.codemadness.org/json2tsv
Log
Files
Refs
README
LICENSE
---
commit 607615a6d80ffa57f81aca02f77ae94960828d63
parent e2eeb2664324786e1a51e0ce64dbe03eeeff8346
Author: Hiltjo Posthuma <[email protected]>
Date: Tue, 30 Aug 2022 20:20:38 +0200
add jtq: a convenience wrapper script
It wraps json2tsv, sets options for handling JSON data in a lossless manner and
uses awk as a "query language".
Diffstat:
M Makefile | 16 +++++++++-------
M json2tsv.1 | 4 ++--
A jtq | 31 +++++++++++++++++++++++++++++…
A jtq.1 | 46 +++++++++++++++++++++++++++++…
4 files changed, 88 insertions(+), 9 deletions(-)
---
diff --git a/Makefile b/Makefile
@@ -19,9 +19,11 @@ JSON2TSV_CPPFLAGS = -D_DEFAULT_SOURCE
#JSON2TSV_CPPFLAGS = -D_DEFAULT_SOURCE -DGETNEXT=getchar
BIN = ${NAME}
+SCRIPTS = jtq
SRC = ${BIN:=.c}
HDR = json.h
-MAN1 = ${BIN:=.1}
+MAN1 = ${BIN:=.1}\
+ ${SCRIPTS:=.1}
DOC = \
LICENSE\
README
@@ -53,7 +55,7 @@ ${LIBJSON}: ${LIBJSONOBJ}
dist:
rm -rf "${NAME}-${VERSION}"
mkdir -p "${NAME}-${VERSION}"
- cp -f ${MAN1} ${DOC} ${HDR} \
+ cp -f ${MAN1} ${DOC} ${HDR} ${SCRIPTS} \
${SRC} ${LIBJSONSRC} Makefile "${NAME}-${VERSION}"
# make tarball
tar cf - "${NAME}-${VERSION}" | gzip -c > "${NAME}-${VERSION}.tar.gz"
@@ -63,10 +65,10 @@ clean:
rm -f ${BIN} ${OBJ} ${LIB}
install: all
- # installing executable files.
+ # installing executable files and scripts.
mkdir -p "${DESTDIR}${PREFIX}/bin"
- cp -f ${BIN} "${DESTDIR}${PREFIX}/bin"
- for f in ${BIN}; do chmod 755 "${DESTDIR}${PREFIX}/bin/$$f"; done
+ cp -f ${BIN} ${SCRIPTS} "${DESTDIR}${PREFIX}/bin"
+ for f in ${BIN} ${SCRIPTS}; do chmod 755 "${DESTDIR}${PREFIX}/bin/$$f"…
# installing example files.
mkdir -p "${DESTDIR}${DOCPREFIX}"
cp -f ${DOC} "${DESTDIR}${DOCPREFIX}"
@@ -77,8 +79,8 @@ install: all
for m in ${MAN1}; do chmod 644 "${DESTDIR}${MANPREFIX}/man1/$$m"; done
uninstall:
- # removing executable files.
- for f in ${BIN}; do rm -f "${DESTDIR}${PREFIX}/bin/$$f"; done
+ # removing executable files and scripts.
+ for f in ${BIN} ${SCRIPTS}; do rm -f "${DESTDIR}${PREFIX}/bin/$$f"; do…
# removing example files.
for d in ${DOC}; do rm -f "${DESTDIR}${DOCPREFIX}/$$d"; done
-rmdir "${DESTDIR}${DOCPREFIX}"
diff --git a/json2tsv.1 b/json2tsv.1
@@ -1,4 +1,4 @@
-.Dd May 2, 2022
+.Dd August 30, 2022
.Dt JSON2TSV 1
.Os
.Sh NAME
@@ -130,7 +130,7 @@ json2tsv -r -F '\ex1f' -R '\ex1e' < input.json | \e
.Ed
.Sh SEE ALSO
.Xr awk 1 ,
-.Xr grep 1
+.Xr jtq 1
.Sh AUTHORS
.An Hiltjo Posthuma Aq Mt [email protected]
.Sh CAVEATS
diff --git a/jtq b/jtq
@@ -0,0 +1,31 @@
+#!/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 type…
+ --)
+ shift; break;;
+ esac
+done
+
+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}
+fi
diff --git a/jtq.1 b/jtq.1
@@ -0,0 +1,46 @@
+.Dd August 30, 2022
+.Dt JTQ 1
+.Os
+.Sh NAME
+.Nm jtq
+.Nd json2tsv convenience wrapper script using awk as a query language
+.Sh SYNOPSIS
+.Nm
+.Op Fl n
+.Op Ar awk expressions...
+.Sh DESCRIPTION
+.Nm
+reads JSON data from stdin.
+When any
+.Ar awk expressions
+are given then the
+.Xr json2tsv 1
+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
+.Xr json2tsv 1 .
+.Pp
+The options are as follows:
+.Bl -tag -width Ds
+.It Fl n
+Show the indices for array types (by default off).
+.El
+.Sh EXIT STATUS
+.Ex -std
+.Sh EXAMPLES
+.Bd -literal
+echo '{"url":"https://codemadness.org/"}' | \e
+ jtq '$1 == ".url" { print $3 }'
+.Ed
+.Sh SEE ALSO
+.Xr awk 1 ,
+.Xr json2tsv 1
+.Sh AUTHORS
+.An Hiltjo Posthuma Aq Mt [email protected]
You are viewing proxied material from codemadness.org. The copyright of proxied material belongs to its original authors. Any comments or complaints in relation to proxied material should be directed to the original authors of the content concerned. Please see the disclaimer for more details.