1

I would like to remove a username from a .htaccess file.

Example .htaccess:

RewriteEngine On
RewriteBase /~usern/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /~usern/index.php [L]

I want to us a sort of dynamic command in that I don't have to manually type /~username every time for each user.

I tried

sed 's/$whoami##g' .htaccess

but this does not achieve the desired result.

Adrian Frühwirth
  • 42,970
  • 10
  • 60
  • 71
New Basher
  • 23
  • 1
  • 5
  • Possible duplicate of [Remove occurrences of string in text file](https://stackoverflow.com/q/5998454/608639), [How to replace multiple patterns at once with sed?](https://stackoverflow.com/q/26568952/608639), [How can I delete words from each line of a file using sed in shell script?](https://stackoverflow.com/q/43329477/608639), etc. – jww May 31 '19 at 10:42

3 Answers3

3

You could sed -i "s/$(whoami)//g" .htaccess

If you only want to remove the first entry on a line, remove the last g option.

sed -i (inline sed, do actions directly on file) "s/find_this/replace_with_this/" (search and replace)

If you want to search for ~/username and remove that instead of only username entries, just change the expression to:

sed -i "s/~\/$(whoami)//g" .htaccess

Jite
  • 4,250
  • 1
  • 17
  • 18
  • We are close! what if we are trying to remove /~username not ~/username – New Basher Apr 06 '13 at 23:44
  • As simple as it was! I did finally get this thanks to your original command just had to move \ couple spots over. Very much appreciated help! sed -i "s/\/~$(whoami)//g" .htaccess – New Basher Apr 07 '13 at 08:12
  • @NewBasher Ah yeah, sorry. `~/` is *nix way of getting to home directory, but in an url it's ofc `/~user`. Glad you solved it :) – Jite Apr 08 '13 at 11:02
1

you are looking for s in sed:

 sed 's#/~usern##g' file

with your example:

kent$  cat file
RewriteEngine On<br />
RewriteBase /~usern/<br />
RewriteRule ^index\.php$ - [L]<br />
RewriteCond %{REQUEST_FILENAME} !-f<br />
RewriteCond %{REQUEST_FILENAME} !-d<br />
RewriteRule . /~usern/index.php [L]<br />

kent$  sed 's#/~usern##g' file
RewriteEngine On<br />
RewriteBase /<br />
RewriteRule ^index\.php$ - [L]<br />
RewriteCond %{REQUEST_FILENAME} !-f<br />
RewriteCond %{REQUEST_FILENAME} !-d<br />
RewriteRule . /index.php [L]<br />
Kent
  • 189,393
  • 32
  • 233
  • 301
1

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.
F. Hauri - Give Up GitHub
  • 64,122
  • 17
  • 116
  • 137