Format sql files with sed: tab to space, pad commas with spaces, uppercase match

Shell script to make more readable all sql files in a directory – pads commas and brackets with whitespace, converts tabs to spaces, makes SQL keywords uppercase.

# FUNCTIONS
pad () {
    sed -i "s/\([^ ]\)$1/\1 $1/g" *sql
    sed -i "s/$1\([^ ]\)/$1 \1/g" *sql
}
upper () {
    sed -i "s/\(\<$1\>\)/\U\1/g" *sql
}
# MAIN
dos2unix *sql
# tabs to 4 spaces
sed -i 's/\t/    /g' *sql
# pad brackets commas semicolons
pad '('
pad ')'
pad ','
pad ';'
pad '!='
pad '='
pad '||'
# uppercase
upper nvl
upper decode
upper to_char
upper least
upper greatest
upper when
upper then
upper case
upper end
upper values
upper varchar2
upper in
upper date
upper upper
upper regexp_replace
upper sysdate
upper exists
upper replace
upper else
upper substr
upper count
upper distinct
upper partition
upper over
Posted in Uncategorized
One comment on “Format sql files with sed: tab to space, pad commas with spaces, uppercase match
  1. 94Mariana says:

    Hi blogger, i must say you have very interesting posts here.
    Your blog should go viral. You need initial traffic
    boost only. How to get it? Search for: Mertiso’s tips go viral

Leave a Reply

Your email address will not be published. Required fields are marked *

*