Skip to content

Instantly share code, notes, and snippets.

@rahulinux
Created August 28, 2013 16:55
Show Gist options
  • Save rahulinux/6368371 to your computer and use it in GitHub Desktop.
Save rahulinux/6368371 to your computer and use it in GitHub Desktop.
Comment Match tags in configuration files and append new one
# This Function will handal escaped character / also using sed
CommentAppend() {
# Comment line and append line below commented line
local comment="$( echo "$1" | sed 's/\(\/\)/\\\//g' )" # search this line and comment it
local append="$( echo "$2" | sed 's/\(\/\)/\\\//g' )" # Append this line below commented line
local InputFile="$3"
sed -i "s/^${comment}/#${comment}/g" $InputFile
# if string does not exists in input file then add
if ! grep "${append}" $InputFile >/dev/null 2>&1; then
sed -i "s/#${comment}/& \n${append}/" $InputFile
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment