For example, one could separate out the filename sanity stuff
into it's own script in a separate file, like this:

EDIT: added some html escapes for fun.
EDIT: fixed the ordering of a few things.


Code:
#!/usr/bin/gawk -f
{
        ## spaces to underscores:
        gsub(" ","_")

        ## some html escapes:
        gsub(">",">")
        gsub("&lt;","<")
        gsub("&quot;","\"")
        gsub("&amp;","\\&")

        ## square brackets into round brackets:
        gsub("\\[","(")
        gsub("\\]",")")

        ## double-quotes into apostrophes:
        gsub("\"","'")

        ## sanitize the rest:
        gsub("[^- 'a-zA-Z0-9_$+&<>]*","")

        ## dump it to stdout
        print
}


Which could be saved as sanitize.awk, and then be used like this:

Code:
NASTYNAME="whatever.. "
GOODNAME=`echo "$NASTYNAME" | sanitize.awk`



Edited by mlord (13/01/2009 21:46)