1

These are the contents of the text file:

cat
dog
elephant
cat lion
cat tiger
penguin
penguin emperor
zebra

I want to remove the line contain just cat and retain cat lion and cat tiger lines.

There could be nth number of spaces after the word cat in the cat line, it could be zero or 10.

Any ideas how it can be done?

2

It's fairy simple with sed:

sed -e '/^cat$/d' <file

/^cat$/ only match line which contains only cat, then delete that line.

or awk:

awk '!/^cat$/' <file

or grep:

grep -v '^cat$' <file

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.