Reputation: 3135
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?
Upvotes: 10
Views: 15587
Reputation: 181
Multiple string removal using grep
grep -vE "facebook|youtube|google|amazon|dropbox" file.txt > filtered.txt
For case-insensitive string removal using grep
grep -viE "facebook|youtube|google|amazon|dropbox" file.txt > filtered_case_insensitive.txt
Upvotes: 0
Reputation: 23085
Try this:
sed '/facebook\|youtube\|google\|amazon\|dropbox/d' myfile.txt
From GNU's sed manual:
regexp1\|regexp2
Matches either
regexp1
orregexp2
. 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.
Upvotes: 17
Reputation: 77185
With awk
awk '!/facebook|youtube|google|amazon|dropbox/' myfile.txt > filtered.txt
Upvotes: 6
Reputation: 247220
grep -vf wordsToExcludeFile myfile.txt
"wordsToExcludeFile" should contain the words you don't want, one per line.
If you need to save the result back to the same file, then add this to the command:
> myfile.new && mv myfile.new myfile.txt
Upvotes: 9