Skip to content

Instantly share code, notes, and snippets.

@netj
Last active September 21, 2018 14:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save netj/06354c73aa89bd0481f3ff43b26aae79 to your computer and use it in GitHub Desktop.
Save netj/06354c73aa89bd0481f3ff43b26aae79 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
# exshell -- a handy way to use command-line formulae's output to augment a TAB-separated input
#
# Synopsis:
# $ cat a.txt
# USA United States of America
# India Republic of India
# South_Korea Republic of Korea (ROK)
#
# $ exshell <a.txt \
# 'curl -fsSL https://en.wikipedia.org/wiki/"$1" | grep -i "Population" | tail -n +2 | sed "s/<.*$//"' \
# 'curl -fsSL https://en.wikipedia.org/wiki/"$1" | grep -i "Wikidata item" | tr " =" "\n" | grep www.wikidata.org' \
# | column -t -s$'\t'
# USA United States of America 325,719,178 "https://www.wikidata.org/wiki/Special:EntityPage/Q30"
# India Republic of India 1,324,171,354 "https://www.wikidata.org/wiki/Special:EntityPage/Q668"
# South_Korea Republic of Korea (ROK) 51,446,201 "https://www.wikidata.org/wiki/Special:EntityPage/Q884"
#
# TODO improve example to use more than just $1
##
# Author: Jaeho Shin <netj@sparcs.org>
# Madhav Sharan <goyal.madhav@gmail.com>
# Created: 2018-09-19
##
# prepare the commands to run for each given formula
cmds=()
for formula; do
if type "$formula" &>/dev/null || test -x "$formula"; then
# call formula directly when it's an executable passing the input columns as arguments
cmd="$formula"
else # otherwise, treat it as a bash script that uses positional args
cmd="bash -c $(
if test ${BASH_VERSINFO[0]} -ge 4
then echo "${formula@Q}" # using simpler quotes in bash>=4 when possible
else printf %q "$formula"
fi
) --"
fi
cmds+=("$cmd")
done
# compile each input TAB-separated line into a bash script that emits output of the commands as augmented columns
{
echo "IFS=\$'\\t'"
while read; do
printf 'a=%q;set -- $a\npaste <(echo "$*")' "$REPLY"
printf ' <(%s "$@" </dev/null | head -1)' "${cmds[@]}"
echo
done
} |
# then run the compiled script
bash -s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment