Skip to content

Instantly share code, notes, and snippets.

@roder
Created September 30, 2010 19:36
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 roder/605174 to your computer and use it in GitHub Desktop.
Save roder/605174 to your computer and use it in GitHub Desktop.
#!/bin/bash
# This utility is useful for extracting the field names from a SQL to CSV export
#
# csvheadpivot will scan the current working directory for CSV files. When it
# encounters a CSV file the utility assumes the CSV file has a header row and
# any CSV files with more than one row has data.
#
# When encountering a CSV with data, the utility will grab the header row and
# pivot it to a single column.
mkdir ./fields;
echo "Created fields directory in the current working directory."
echo "Searching current working directory for CSV"
echo "Creating fields file in ./fields/:"
for i in `ls *.csv`;
do res=`wc -l $i`;
lc=`echo $res | cut -d ' ' -f 1`;
if [ $lc -gt 1 ];
then
head -1 $i | sed 's:,:\
:g' > ./fields/fields-$i;
echo " fields-$i";
fi
done;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment