Skip to content

Instantly share code, notes, and snippets.

@betafcc
Last active May 7, 2021 03:33
Show Gist options
  • Save betafcc/cbca8b7b24fd5305a543bf498c893ce4 to your computer and use it in GitHub Desktop.
Save betafcc/cbca8b7b24fd5305a543bf498c893ce4 to your computer and use it in GitHub Desktop.
Easily uploads a csv to mysql via stdin
#!/usr/bin/env bash
set -euo pipefail
SEP="${SEP:=,}"
TER="${TER:=\n}"
TABLE="${1}"; shift # first positional argument is the tablename
CMD="${@}" # the rest is the mysql command
# examples:
# cat myfile.csv | csv-mysql mytable mysql -Dtest -uroot --ppassword
# cat myfile.tsv | SEP='\t' csv-mysql mytable mysql -Dtest -uroot --ppassword
# wget -O - https://foo.com/my_remote_zipped.csv.gz | zcat | SEP='\t' csv-mysql mytable mysql -Dtest -uroot --ppassword
main() {
# capture the first line
read -r columns
# transform in table declaration and run it
echo "${columns}" | table "${TABLE}" | ${CMD}
# send the rest of stdin to mysql running load infile from stdin
${CMD} --execute="$(load_rows ${TABLE})"
}
table() {
echo -e "CREATE TABLE ${1} (\n$(fields)\n);"
}
fields() {
echo -e ' `'"$(sed -E 's/'"${SEP}"'/` TEXT,\n `/g')"'` TEXT'
}
load_rows() {
echo -e "
LOAD DATA LOCAL INFILE
'/dev/stdin'
INTO TABLE
${1}
FIELDS TERMINATED BY
'${SEP}'
LINES TERMINATED BY
'${TER}';
"
}
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment