开发者

Remove occurrences of string in text file

开发者 https://www.devze.com 2023-03-06 01:09 出处:网络
How would you remove a specific string from a t开发者_JAVA技巧ext file (command line) e.g. hello goodbye

How would you remove a specific string from a t开发者_JAVA技巧ext 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)


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

To delete multiple words:

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


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

Even if you're using a Windows machine, you can download and configure the Ubuntu shell and use the above commands.

0

精彩评论

暂无评论...
验证码 换一张
取 消