Sed Replace A Multi-Line String

From Chorke Wiki
Revision as of 03:12, 24 December 2022 by Shahed (talk | contribs) (Created page with "{| |valign="top" colspan="2"| <source lang="bash"> mkdir -p ${HOME}/Documents/sed_playground sudo tee -a ${HOME}/Documents/sed_playground/000-default.conf >/dev/null <<EOF...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search
mkdir    -p ${HOME}/Documents/sed_playground
sudo tee -a ${HOME}/Documents/sed_playground/000-default.conf >/dev/null <<EOF
#
#This is some test comments
#    Skip this
#

<Directory "/var/www/cgi-bin">
    AllowOverride None
    Options +ExecCGI
    AddHandler cgi-script .cgi .pl
    Require all granted
</Directory>
EOF

CONF_FILE="${HOME}/Documents/sed_playground/000-default.conf"
DIRECTORY_FIND_LEAD_EXP='<Directory "\/var\/www\/cgi-bin">'

DIRECTORY_FIND_FULL_EXP=$(cat <<EOF
${DIRECTORY_FIND_LEAD_EXP}\n\
[ ]*AllowOverride None\n\
[ ]*Options +ExecCGI\n\
[ ]*AddHandler cgi-script \.cgi \.pl\n\
[ ]*Require all granted\n\
<\/Directory>
EOF
)
DIRECTORY_FILL_FULL_EXP=$(cat <<EOF
#<Directory "\/var\/www\/cgi-bin">\n\
    #AllowOverride None\n\
    #Options +ExecCGI\n\
    #AddHandler cgi-script \.cgi \.pl\n\
    #Require all granted\n\
#<\/Directory>
EOF
)

echo ""
echo "Sedding"
sed -i "/${DIRECTORY_FIND_LEAD_EXP}/{
    N;N;N;N;N
    s|${DIRECTORY_FIND_FULL_EXP}|${DIRECTORY_FILL_FULL_EXP}|
}" ${CONF_FILE}

#
#This is some test comments
#    Skip this
#

<Directory "/var/www/cgi-bin">
    AllowOverride None
    Options +ExecCGI
    AddHandler cgi-script .cgi .pl
    Require all granted
</Directory>
#
#This is some test comments
#    Skip this
#

#<Directory "/var/www/cgi-bin">
    #AllowOverride None
    #Options +ExecCGI
    #AddHandler cgi-script .cgi .pl
    #Require all granted
#</Directory>