bash sed 查找和删除单个单词

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15855735/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 05:07:45  来源:igfitidea点击:

Sed find and delete single word

bashsed

提问by New Basher

I would like to remove a username from a .htaccessfile.

我想从.htaccess文件中删除用户名。

Example .htaccess:

示例.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 /~usernameevery time for each user.

我想要一种动态命令,因为我不必/~username每次都为每个用户手动输入。

I tried

我试过

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

but this does not achieve the desired result.

但这并没有达到预期的效果。

回答by Jite

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

你可以 sed -i "s/$(whoami)//g" .htaccess

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

如果您只想删除一行中的第一个条目,请删除最后一个g选项。

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

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:

如果您想搜索 ~/username 并删除它而不仅仅是用户名条目,只需将表达式更改为:

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

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

回答by F. Hauri

Instead of droppingold 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 copyof the old line (and ensure to modify only uncommented desired lines):

因此,与其替换,我更喜欢制作旧行的注释副本(并确保仅修改未注释的所需行):

sed -re "s/^(\s*Rewrite.*)\/~$(whoami)(.*$)/# &\n/" -i.bak .htaccess

Than diff -u .htaccess{.bak,}will produce:

diff -u .htaccess{.bak,}将产生:

--- .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 newlineand the same line without usern

查找包含Rewrite.*usern. 用注释标记 ( #)替换这一行,然后是整行,而不是 anewline和没有的同一行usern

Because \s*is inthe parenthesis, any kind of indentation will be reproduced at output.

因为\s*括号中,任何种类的压痕将在输出中再现。

When sedis run with the switch -i.bak, older unmodified version of file will be stored as .htaccess.bak(This could be a little redundantwith the syntaxe /# &\n...who would duplicate and comment unmodified old lines).

sed使用 switch 运行时-i.bak,旧的未修改版本的文件将被存储为.htaccess.bak(这对于复制和注释未修改的旧行的语法来说可能有点多余/# &\n...)。

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.
  • 编辑时备份旧文件
  • 编辑时备份旧行
  • 编辑时备份行和文件
  • 什么都不备份,因为您已经拥有强大而高效的备份解决方案。

回答by Kent

you are looking for sin sed:

s在 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 />