Linux 删除文本文件中出现的字符串

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

Remove occurrences of string in text file

linuxwindowstextcommand-line

提问by James

How would you remove a specific string from a text file (command line) e.g.

您将如何从文本文件(命令行)中删除特定字符串,例如

hello
goodbye
goodbye
hello
hello
hello
goodbye

In this case I would like to remove all occurrences of "goodbye"

在这种情况下,我想删除所有出现的“再见”

Either linux or Windows, (as longs as the linux command is available in GNU)

linux 或 Windows,(只要 linux 命令在 GNU 中可用)

采纳答案by linuts

sed -i -e 's/goodbye//g' filename

To delete multiple words:

删除多个单词:

sed -i -e 's/\(goodbye\|hello\|test\|download\)//g' filename

回答by Vishwas Shenoy Alevoor

If you are looking for Linux file through Vi mode. Please find below solution which helped me out

如果您正在通过 Vi 模式查找 Linux 文件。请找到以下对我有帮助的解决方案

:g/goodbye/d - This will delete all goodbye's

:g/goodbye/d - 这将删除所有再见的

:%g!/goodbye/d - This will delete all except goodbye's

:%g!/goodbye/d - 这将删除除再见之外的所有内容