#!/bin/bash #fmt_sql.sh: Format a long SQL of multiple lines into a readable and runnable SQL #Please pass of the file storing the unformatted SQL as argument and the SQL in it will be formatted. The file named .bk will store its original SQL. #If the SQL is a DML, you may manually shift the word "insert", "delete", or "update" to the beginning of line. #If you want the line length to be other than 100, change the number 100 on the line of `fold' below. cp -p $1 $1.bk perl -pi -e 's/\n//g;s/(\bSELECT\b)/\n$1/ig;s/(\bFROM\b)/\n$1/ig;s/(\bWHERE\b)/\n$1/ig' $1 fold -w 100 -s $1 > $$ perl -pi -e 's/^/ / unless /(^SELECT|^FROM|^WHERE)/i' $$ mv $$ $1