perl -p -i -e 's%^(include /etc/nginx/https.include;)$%#$1%' /etc/nginx/ap-servers.include
If the line might not end in the ;
, use instead:
perl -p -i -e 's%^(include /etc/nginx/https.include;.*)$%#$1%' /etc/nginx/ap-servers.include
If you want to preserve the original file, add a backup extension after -i
:
perl -p -i.bak -e 's%^(include /etc/nginx/https.include;)$%#$1%' /etc/nginx/ap-servers.include
Now, explaining. The -p
flag means replace in-place. All lines of the file will be fed to the expression, and the result will be used as replacement. The -i
flag indicates the extension of the backup file. By using it without anything, you prevent generation of backups. The -e
tells Perl to get the following parameter as an expression to be executed.
Now, the expression is s%something%other%
. I use %
instead of the more traditional /
to avoid having to escape the slashes of the path. I use parenthesis in the expression and $1
in the substituted expression for safety -- if you change one, the other will follow. Thus, %#$1%
is actually the second %
of s
, followed by the desired #
, $1
indicating the pattern inside parenthesis, and the last %
of s
.
HTH. HAND.