Instead of dropping old version, why not store them as comment:
When I do some modif on configuration files, I like to do a lot of backups, in order to be able to get older configuration if something wrong.
So, instead of replacing, I prefer to make a commented copy of the old line (and ensure to modify only uncommented desired lines):
sed -re "s/^(\s*Rewrite.*)\/~$(whoami)(.*$)/# &\n\1\2/" -i.bak .htaccess
Than diff -u .htaccess{.bak,}
will produce:
--- .htaccess.bak 2013-09-07 18:44:07.043537404 +0200
+++ .htaccess 2013-09-07 18:46:35.236454686 +0200
@@ -1,6 +1,8 @@
RewriteEngine On<br />
-RewriteBase /~usern/<br />
+# RewriteBase /~usern/<br />
+RewriteBase /<br />
RewriteRule ^index\.php$ - [L]<br />
RewriteCond %{REQUEST_FILENAME} !-f<br />
RewriteCond %{REQUEST_FILENAME} !-d<br />
-RewriteRule . /~usern/index.php [L]<br />
+# RewriteRule . /~usern/index.php [L]<br />
+RewriteRule . /index.php [L]<br />
Explanation:
Finding a line containing Rewrite.*usern
. Replacing this line by a comment mark (#
), followed by the entire line, than a newline
and the same line without usern
Because \s*
is in the parenthesis, any kind of indentation will be reproduced at output.
When sed
is run with the switch -i.bak
, older unmodified version of file will be stored as .htaccess.bak
(This could be a little redundant with the syntaxe /# &\n...
who would duplicate and comment unmodified old lines).
It's only for sample, so you have choice:
- backup old files while editing
- backup old lines while editing
- backup both lines and files while editing
- backup nothing as you already have a strong and efficient backup solution.