Use json2tsv instead of jq - ud - Print definitions from urbandictionary.com
(HTM) hg clone https://bitbucket.org/iamleot/ud
(DIR) Log
(DIR) Files
(DIR) Refs
(DIR) README
---
(DIR) changeset 2dba44274100a85a452cfb3859442f9b1c25a73c
(DIR) parent e47fcbb60b06f9d2a134565df02d404267603407
(HTM) Author: Leonardo Taccari <iamleot@gmail.com>
Date: Thu, 28 Nov 2019 15:05:12
Use json2tsv instead of jq
Diffstat:
README | 2 +-
ud.sh | 32 ++++++++++++++++++++++++++------
2 files changed, 27 insertions(+), 7 deletions(-)
---
diff -r e47fcbb60b06 -r 2dba44274100 README
--- a/README Wed Sep 19 17:45:46 2018 +0200
+++ b/README Thu Nov 28 15:05:12 2019 +0100
@@ -4,4 +4,4 @@
% ud timtowtdi
TIMTOWTDI: "There is more than one way to do it"
-To use `ud` `curl` and `jq` are needed.
+To use `ud` `curl` and `json2tsv` are needed.
diff -r e47fcbb60b06 -r 2dba44274100 ud.sh
--- a/ud.sh Wed Sep 19 17:45:46 2018 +0200
+++ b/ud.sh Thu Nov 28 15:05:12 2019 +0100
@@ -1,7 +1,7 @@
#!/bin/sh
#
-# Copyright (c) 2017 Leonardo Taccari
+# Copyright (c) 2017-2019 Leonardo Taccari
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
@@ -32,9 +32,29 @@
for w in "$@"; do
curl -s "${ud_api_url}${w}" | \
- jq -r \
- '.list[] |
- .word + ": " +
- .definition + "\n"' | \
- fmt
+ json2tsv | awk -F '\t' '
+ BEGIN {
+ bn = ".list[]"
+ }
+
+ $1 == bn ".word" {
+ n++
+ word = $3
+ gsub(/\\n/, "\n", word)
+ }
+
+ $1 == bn ".definition" {
+ definition = $3
+ gsub(/\\n/, "\n", definition)
+ }
+
+ word && definition {
+ if (n > 1) {
+ printf "\n"
+ }
+ print word ": " definition
+ word = definition = ""
+ }
+ ' |
+ fmt
done