Skip to content

Instantly share code, notes, and snippets.

@timwis
Created September 20, 2019 15:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timwis/1ba8e424e27c6c9dfcd97c59426bc913 to your computer and use it in GitHub Desktop.
Save timwis/1ba8e424e27c6c9dfcd97c59426bc913 to your computer and use it in GitHub Desktop.
Import and export CSV files with orc
#!/usr/bin/env bash
import () {
csvjson "${3:-/dev/stdin}" \
| jq --raw-output '.[] | @sh "echo orc add-entry '"$1"' user \(.'"$2"') \(tostring)"' \
| xargs -0 bash -c
}
export_ () {
orc ls "$1" user | cut -f 4 | in2csv -f ndjson
}
usage () {
cat <<EOF
usage:
orcsv import <REGISTER> <KEYFIELD> <FILENAME>
cat <FILENAME> | orcsv import <REGISTER> <KEYFIELD>
orcsv export <REGISTER>
dependencies:
orc: https://github.com/register-dynamics/orc
csvkit: https://csvkit.rtfd.org
jq: https://stedolan.github.io/jq/
EOF
}
case $1 in
import) shift; import "$@";;
export) shift; export_ "$@";;
*) usage; exit 1;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment