Skip to content

Instantly share code, notes, and snippets.

@l04m33
Created April 16, 2014 16:55
Show Gist options
  • Save l04m33/10906622 to your computer and use it in GitHub Desktop.
Save l04m33/10906622 to your computer and use it in GitHub Desktop.
#!/bin/bash
function get_new_names() {
local input_names=$1
sed 's/[ \t]\+/:/g' $input_names
}
function gen_subst_func() {
local from=$1
local to=$2
local func_name=$3
eval "
function $func_name () {
sed \"s/$from/$to/g\"
}"
}
main() {
local input_names=$1
if [ -z "$input_names" ]; then
echo "Usage: $0 <config file>" 1>&2
exit 1
elif [ ! -e "$input_names" ]; then
echo "File \"$input_names\" not found" 1>&2
exit 1
fi
local new_names=$(get_new_names $input_names)
local i=1
local chain_str=''
local func_name=''
for nn in $new_names; do
gen_subst_func "${nn%%:*}" "${nn##*:}" "subst_func_$i"
chain_str="$chain_str | subst_func_$i"
i=$(expr $i + 1)
done
eval "cat $chain_str"
}
main $@
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment