Last active
September 21, 2018 14:23
-
-
Save netj/06354c73aa89bd0481f3ff43b26aae79 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 <[email protected]> | |
# Madhav Sharan <[email protected]> | |
# 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