Created
April 9, 2011 19:24
-
-
Save fabito/911692 to your computer and use it in GitHub Desktop.
Generating CSV files using sqlcmd and groovy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def username = args[0] | |
def password = args[1] | |
def host = args[2] | |
def database = args[3] | |
def dir = './' | |
def ant = new AntBuilder() | |
def p = ~/.*\.sql/ | |
new File( dir ).eachFileMatch(p) { f -> | |
def sqlFile = f.name | |
def tempCsvFile = sqlFile.replaceAll(/.sql/,'') | |
def cmd = "sqlcmd -S $host -U $username -P $password -d ${database} -i ${sqlFile} -W -o ${tempCsvFile}.temp -s ;" | |
def process = cmd.execute() | |
process.waitFor() | |
ant.move(file: "${tempCsvFile}.temp", tofile:"${tempCsvFile}.csv", overwrite: true ) { | |
filterchain(){ | |
headfilter(lines:"-1", skip: "2") | |
tailfilter(lines: "-1", skip: "2" ) | |
ignoreblank() | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment