Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete line containing one of multiple strings

Tags:

text

bash

sed

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

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?

like image 893
user1899415 Avatar asked Jun 11 '13 17:06

user1899415


2 Answers

Try this:

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

From GNU's sed manual:

regexp1\|regexp2

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.

like image 147
gpojd Avatar answered Oct 26 '22 03:10

gpojd


With awk

awk '!/facebook|youtube|google|amazon|dropbox/' myfile.txt > filtered.txt
like image 45
jaypal singh Avatar answered Oct 26 '22 03:10

jaypal singh