删除包含多个字符串之一的行 [英] Delete line containing one of multiple strings

查看:68
本文介绍了删除包含多个字符串之一的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文本文件,我想删除所有包含以下单词的行: facebook youtube google amazon投递箱

I have a text file and I want to remove all lines containing the words: facebook, youtube, google, amazon, dropbox, etc.

我知道要删除包含sed字符串的行:

I know to delete lines containing a string with sed:

sed '/facebook/d' myfile.txt

我不想为每个字符串运行五次不同的命令,有没有办法将所有字符串组合成一个命令?

I don't want to run this command five different times though for each string, is there a way to combine all the strings into one command?

推荐答案

尝试一下:

sed '/facebook\|youtube\|google\|amazon\|dropbox/d' myfile.txt

GNU的sed手册:

regexp1 \ | regexp2

regexp1\|regexp2

匹配 regexp1 regexp2 .使用括号来使用复杂的替代正则表达式.匹配过程尝试每个备选方案依次从左到右,第一个备选方案是使用成功.这是GNU扩展.

Matches either regexp1 or regexp2. Use parentheses to use complex alternative regular expressions. The matching process tries each alternative in turn, from left to right, and the first one that succeeds is used. It is a GNU extension.

这篇关于删除包含多个字符串之一的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆