I want to remove newline \n
only from end of the file in unix
e.g.
abc
def
ghi
output should be
abc
def
ghi
In the end of file there should not be a single \n
I want to remove newline \n
only from end of the file in unix
e.g.
abc
def
ghi
output should be
abc
def
ghi
In the end of file there should not be a single \n
You can:
perl -pe 'chomp if eof' file1 > file2
Example:
$ cat file1
abc
def
ghi
$ perl -pe 'chomp if eof' file1 > file2
$ cat file2
abc
def
ghi$
Generally, Unix text tools are happier if you do have a newline at the end of the last line in the file. Why do you need to remove it?
You can't do this (as far as I know) with awk
, but it's easy with perl
:
perl -e 'undef $/; $_ = <>; s/\R\z//; print'
EDIT: Years later it occurs to me that you might have meant "how do I delete trailing blank lines from the end of the file"; in other words, you might have wanted to reduce two or more consecutive newlines to a single one, only at the end of the file. Again that is easiest with perl
:
perl -e 'undef $/; $_ = <>; s/\s+\z/\n/; print'
(This will zorch arbitrary trailing whitespace characters, not just newlines. This is almost certainly what you want.)