Jq group_by and split into file names

technotes
Apr 12, 2021

curl -s https://gowatchit.net/data/vaccine/counties/CA.json | jq -cr ‘group_by(.fips) | .[] | .[0].fips as $k | “\($k)\t\(.)”’ | awk -F\\t ‘{ fname=$1".json”; print $2 > fname; close(fname) }’

  • -c puts output of a list on a line of its own.
  • group-by fips
  • before it gets to awk: first column is fips, second column is the each group
  • Awk script use the first column as name, and second column as data. It’s tab separated.

Awk can also be use to launch another command with system

cat README.md |awk ‘{system(“date”); print $1}’

echo $’a\thello world\nb\nc’ |awk -F\\t ‘{system($1.b); print $1, $2}’

Good jq tutorial: https://earthly.dev/blog/jq-select/

--

--