« Using an axis client to call fedora modifyDatastreamByValue method | Main | PDF files that are not being indexed SQL to look at the issue »

script to take a sql query and turn it into a tab delimited file

This will convert an sql query to a tab delimited file. I am sure there is a more elegant way to do this, but this works.
# Name sql2tab.sh
#
# dbFlavor - dspace_sr or dspace_ir
# sql_query - a query for the database
#
# Example:
#  ./sql2tab.sh dspace_ir 'select handle from handle;'
#
# Bug the '*' in an sql query produces an error because bash
# expands it as an las of the directory
#
# J. Silvis
# June 2010
#*******************************************************
flavor=$1
sql_query=$2

echo $sql_query > sql_temp_junk
# run the sql queryy and put it in tempData
psql -U $flavor $flavor  < sql_temp_junk  > tempData

# Replace the pipe (|) delimiter with tab
perl -p -i -e 's/\s+\|\s+/\t/g' tempData
#clear out the leading whitespace
perl -p -i -e 's/^(\s+)(.*)/\2/g' tempData
# dump the results out to be either directly viewed or sent to a pipe.
cat tempData
# get rid of junk files
rm tempData
rm sql_temp_junk


Post a comment

(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)