Skip to content

Instantly share code, notes, and snippets.

@teabot
Last active December 18, 2015 15:39
Show Gist options
  • Save teabot/5805764 to your computer and use it in GitHub Desktop.
Save teabot/5805764 to your computer and use it in GitHub Desktop.
Generated create table DDL from header rows of delimited files.
#!/bin/bash
delim=$1
for file in *.tsv
do
name=${file%.tsv}
columns=`head -n1 $file`
echo -n "CREATE TABLE $name ("
arr=$(echo $columns | tr $delim "\n")
first=1
for column in $arr
do
if [ $first -eq 1 ] ; then nl="" ; pk=" primary key" ; nu="not null" ; else nl=","; pk="" ;nu="null" ; fi
echo $nl
echo -n " $column text $nu$pk"
first=0
done
echo ""
echo ");"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment