Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ejhayes/6889636 to your computer and use it in GitHub Desktop.
Save ejhayes/6889636 to your computer and use it in GitHub Desktop.
Colorizes output, reads from file or stdin, runs sed replacement on incoming input.
#!/bin/bash
set -e
function warn {
# Yellow'ish
echo -e "\033[1;33m$1\033[0m" 1>&2
}
function info {
# Blue'ish
echo -e "\033[1;36m$1\033[0m" 1>&2
}
function error {
# Red'ish
echo -e "\033[1;35m$1\033[0m" 1>&2
}
function usage {
warn "Usage: $0 [file] (reads from STDIN if [file] not provided)"
exit 1
}
function fail {
error "[ERROR]: $1"
usage
}
# Simplify escaping of characters here
function regex_replace {
echo "s/$(echo $1 | sed -e 's/\([[\/.*]\|\]\)/\\&/g')/$(echo $2 | sed -e 's/[\/&]/\\&/g')/g"
}
# This function gets the file contents
# from the first argument or from the
# pipe
function get_file_contents {
if [ $# -eq 1 ]; then
if [ -f $1 ]; then
info "Reading from: $1"
cat $1
else
fail "$1 is not a valid file"
fi
else
info "READING FROM STDIN"
cat
fi
}
# Must specify file
get_file_contents $* | sed -e $(regex_replace "UNESCAPED_REGEX" "UNESCAPED_REGEX") \
-e $(regex_replace "UNESCAPED_REGEX" "UNESCAPED_REGEX")
#!/bin/bash
# Unzip, text replace, and restore
gunzip -c /path/to/zipped_db.sql.tar.gz | read_from_file_or_pipe_and_perform_regex_replacement.sh | mysql -u user -p WHICH_DB
#!/bin/bash
# Dump database, clean input, and zip up
mysqldump -u user -p WHICH_DB | read_from_file_or_pipe_and_perform_regex_replacement.sh | gzip -9 > database_backup_sanitized.sql.tar.gz
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment