Remove occurrences of string in text file

2020-03-13 17:11发布

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)

2条回答
冷血范
2楼-- · 2020-03-13 17:44

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

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

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

查看更多
老娘就宠你
3楼-- · 2020-03-13 18:02
sed -i -e 's/goodbye//g' filename

To delete multiple words:

sed -i -e 's/\(goodbye\|hello\|test\|download\)//g' filename
查看更多
登录 后发表回答