Normalizing Filenames and Data with Bash

778

URLify: convert letter sequences into safe URLs with hex equivalents.

This is my 155th column. That means I’ve been writing for Linux Journal for:


$ echo "155/12" | bc
12

No, wait, that’s not right. Let’s try that again:


$ echo "scale=2;155/12" | bc
12.91

Yeah, that many years. Almost 13 years of writing about shell scripts and lightweight programming within the Linux environment. I’ve covered a lot of ground, but I want to go back to something that’s fairly basic and talk about filenames and the web. …

So purely as an exercise in scripting, let’s write a script that converts any string you hand it into a “web-safe” sequence. Before starting, however, pull out a piece of paper and jot down how you’d solve it.

Normalizing Filenames for the Web

My strategy is going to be easy: pull the string apart into individual characters, analyze each character to identify if it’s an alphanumeric, and if it’s not, convert it into its hexadecimal ASCII equivalent, prefacing it with a “%” as needed.

Read more at Linux Journal