in reply to Re: Way to remove the new line characters at the end of the content in a file
in thread Way to remove the new line characters at the end of the content in a file

Lines containing e.g. a lone } will be removed with that. I'd do this:

perl -ni.bak -le "print if $_" my_file.txt

update: Oops! choroba is right below. Heck...

perl -ni.bak -le "print if length $_" my_file.txt
perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'

Replies are listed 'Best First'.
Re^3: Way to remove the new line characters at the end of the content in a file
by choroba (Cardinal) on Jul 22, 2015 at 10:16 UTC
    The last line containing 0 and no newline would be removed :-)
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re^3: Way to remove the new line characters at the end of the content in a file
by Laurent_R (Canon) on Jul 22, 2015 at 11:25 UTC
    Lines containing e.g. a lone } will be removed with that.
    Oops, that's right, but I based the one-liner on the sample data shown by the OP, i.e. a file with a list of server names, which ought to contain some alphanumerical characters (and/or empty lines), not Perl or C code.

    Perhaps a stricter version:

    perl -ni.bak -e "print $_ unless /^[\n\s]+$/;" my_file.txt
    But even this might also fail on some edge cases, the really right regex really depends on the actual content of the file being processed.