Skip to content

Instantly share code, notes, and snippets.

@dale-c-anderson
Created August 16, 2016 16:15
Show Gist options
  • Save dale-c-anderson/3146327ae8264564ac74d39d0bc65b3a to your computer and use it in GitHub Desktop.
Save dale-c-anderson/3146327ae8264564ac74d39d0bc65b3a to your computer and use it in GitHub Desktop.
Strips comments from config files. Can be used with filename arguments, or with pipes.
#!/bin/bash
function main () {
if [ $# -gt 0 ]; then
# Someone sent us a filename
cat "$@" | grep -v ^\; | grep -v ^$ | grep -v ^\ $ | grep -v '^#'
else
if [ -t 0 ]; then
# Someone called us without args or pipes.
usage
else
# Someone piped some stdin, or the contents of a file to us
DATA="$(cat)"
echo "$DATA" | grep -v ^\; | grep -v ^$ | grep -v ^\ $ | grep -v '^#'
fi
fi
}
function usage () {
echo "Usage:"
echo "$(basename "$0") /path/to/file1 [/path/to/file2 ...]"
echo "cat /path/to/file | $(basename "$0")"
}
main "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment