Bash - 在文件中查找关键字并删除其行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7050717/
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-09 20:53:07  来源:igfitidea点击:

Bash - find a keyword in a file and delete its line

bashsedgrepcommand-line-interfacereplace

提问by Roger

I'd like to give a keyword, find the line where this keyword a?pears in a file and erase the entire line.

我想给出一个关键字,找到这个关键字 a? 在文件中出现的行并擦除整行。

This is what I got but it is not working as it should:

这是我得到的,但它没有正常工作:

KEYWORD='domain.com'
cat /etc/hosts | grep -v "$KEYWORD" > /etc/hosts

UPDATE

更新

This is what worked for me:

这对我有用:

sed -i_bak -e '/domain\.com/d' /etc/hosts

sed -i_bak -e '/domain\.com/d' /etc/hosts

However, as I had two lines with "domain.com", is there a way to tell sed to erase only the line where the exact keyword "domain.com" appears

但是,由于我有两行带有“domain.com”的行,有没有办法告诉 sed 只删除出现确切关键字“domain.com”的行

This is the original content of /etc/hosts:

这是/etc/hosts的原始内容:

127.0.0.1       localhost       localhost.localdomain
222.111.22.222      hvn.domain.com
222.111.22.222      domain.com

Here's how it end up after the command sed -i_bak -e '/domain\.com/d' /etc/hosts:

这是命令后的结果sed -i_bak -e '/domain\.com/d' /etc/hosts

127.0.0.1       localhost       localhost.localdomain

I tried sed -i_bak -e '/\<namourphoto\.com\.br\>/d' ~/Desktop/hostsbut it didn't work.

我试过了,sed -i_bak -e '/\<namourphoto\.com\.br\>/d' ~/Desktop/hosts但没有用。

CONCLUSION

结论

This is the code I came up with (based on the help the fine folks have given me):

这是我想出的代码(基于优秀的人给我的帮助):

D=domain.com
DOMAIN=`echo "$D" | sed 's/\./\\./g'`
sed -i_bak -e "/[\t]$DOMAIN/d" /etc/hosts

Note that:

注意:

  • I am counting that there is always a tab before the domain to be erased

  • I am automatically escaping the dots of the domain name.

  • 我在计算要擦除的域之前总是有一个选项卡

  • 我正在自动转义域名的点。

回答by miku

Use the stream editor, sed:

使用流编辑器sed:

sed -i ".bak" '/culpa/d' test.txt

The above will delete lines containing culpain test.txt. It will create a backup of the original (named test.txt.bak) and will modify the original file in-place.

以上将删除test.txt 中包含culpa 的行。它将创建原始文件(名为 test.txt.bak)的备份,并将就地修改原始文件。

回答by sidyll

Pipe it to another file, not the same one that you're reading from, and be careful with the useless use of cat.

将其通过管道传输到另一个文件,与您正在读取的文件不同,并小心无用的cat.

grep -v "$KEYWORD" /etc/hosts > newfile


Apart from the fine answer given regarding sed, you can also use Perl and a little improved regex to solve this:

除了关于 sed 给出的很好的答案之外,您还可以使用 Perl 和一些改进的正则表达式来解决这个问题:

perl -pi.old -e 's/.*\sdomain\.com\s*\n//' file

Notice I'm considering domain.comwill be isolated by space characters (tabs or spaces, and so on), and that nothing but zero or more spaces will appear after it until the newline. Similarly to -iin sed,
-i.oldin perl sets the $^Ivariable, and your original file will receive the changes while a copy will be kept under the old name and a .oldappended.

请注意,我正在考虑domain.com将被空格字符(制表符或空格等)隔离,并且在它之后直到换行符之前只会出现零个或多个空格。与-i在 sed 中类似,
-i.old在 perl 中设置$^I变量,您的原始文件将接收更改,而副本将保留在旧名称下并.old附加。

回答by Dotti

If you want to only delete last line of your example file

如果您只想删除示例文件的最后一行

sed -i '/[[:space:]]\+domain\.com/d' test.txt