Herr Bischoff


How to Create a Valid Filename From a Text String in POSIX Shell Script

This is a surprisingly involved operation that took some research to write the script successfully. It once again demonstrates both the power of pipes and the resulting compound commands as well as the quirks that core UNIX tools exhibit along with their somewhat archaic handling of regular expressions.

I’ve opted for using awk, sed and tr in a single pipe to keep this readable and to be able to understand what’s going on a year from today. Without a doubt, more concise solutions exist. I always prefer readable over clever.

$title="Some text that's not a (valid) filename - as is!"
echo $title | \
    tr '[:upper:]' '[:lower:]' | \
    tr ' ' '-' | \
    sed -E "s/[\,\.\"\'\!\@\#\$\%\^\&\*\(\)\{\}\=\;\:\<\>\/\?\`\~]//g" | \
    tr -d '\[' | \
    tr -d '\]' | \
    sed -E "s/-{2,}/-/g" | \
    awk '{$1=$1;print}'

The above approach focuses on the most common ASCII characters used in written text and adjusts for shortcomings of sed and tr. The final awk statement is present for brevity, as this is the shortest solution I’ve found to trim preceding and trailing whitespace.