Last active
December 25, 2015 00:39
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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") | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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