0

I have a file with this content: dskldfsd.com

domain

domain.com

sub.domain.com

domain.main.com

anythinghere

I want to delete the line which contains the word domain. Using grep, this command find the exact line I want:

grep -w '^domain$' test.txt

But I want to delete this line that contains domain as a single word from the beginning to the end of the line. I want to keep the lines where the word domain is just part of the string.

1

Use sed:

sed -i -e '/^domain$/d' test.txt
0

How about

grep -v '^domain$' test.txt
0

Why grep -v '^domain$'.As per question its simple grep -v 'domain' as per my understanding.

  • "I want to keep the lines where the word domain is just part of the string. " – RudiC Aug 11 '18 at 19:56
0

Assuming you would want to use standard tools, you could do

grep -xF -v 'domain' test.txt >newtest.txt

The -x flag to grep forces the match to be across a whole line, and -F makes grep treat the pattern as a fixed string rather as a regular expression. The -v inverts the sense of the match so that you in the end get all the lines in test.txt that is not just the string domain. The result in written to newtest.txt.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.