in reply to delete last CR and LF from txt

The following will remove all trailing empty lines.
perl -i.bak -nle"while ($empty) { --$empty; print '' } if (length()) { + print } else { ++$empty }" filename

For example,

foo CR LF bar CR LF CR LF CR LF
will become
foo CR LF bar CR LF