remove lines that contain a specific text in a file

I have a file with a list of emails in it and each line has an email in it. I want to remove lines that contain the string example.com or test.com.

I tried this:

sed -i 's/example.com//ig' file.txt

But it will remove only the string example.com, how can I remove the entire line?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

With GNU sed:

sed '/example.com/d;/test.com/d' -i file.txt

will remove the lines with example.com and test.com.

From man sed:

d      Delete pattern space.  Start next cycle.

Method 2

POSIXly, you can use grep:

grep -Eiv '(example|test).com' <in >out

Method 3

Your regex only matches example.com and sed replaces only example.coms with empty string. Your regex should match any line containing example.com or test.com

sed -i '<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4330036d">[email protected]</a>*(test|example).com.*@@i' file.txt


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x