How to remove a specific line from a file only if there are other lines in the file

perltext processing

How can I remove a specific line from a file ONLY if there are other lines in the file?

For example, don't touch this file:

cat file.txt
ASDF

but remove "ASDF" from this file:

cat file.txt
ASDF
TR422

Because it has other lines than "ASDF".

I'll welcome a solution in bash, perl or any other common tool.

Best Answer

In bash, using sed:

if [[ $(< "$file") != "ASDF" ]]; then
  sed -i '/^ASDF$/d' "$file"
fi
Related Question