8

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

user123444555621
  • 148,182
  • 27
  • 114
  • 126
jn_sum
  • 81
  • 1
  • 1
  • 3
  • awk '{if (NR==1 && $0 ~/>/){print$0;next}if($0~/^>/){print"\n"$0;next}else{printf("%s",$0)}}' filename I tried the above script but it is removing all \n but i want the \n which exist at end of file should be removed. – jn_sum Dec 26 '11 at 11:21
  • What language? What platform? – Oded Dec 26 '11 at 11:21
  • unix, I tried with awk command script – jn_sum Dec 26 '11 at 11:22
  • sed -i '$d' filename, from the given script i am able to remove \n from end of file, but only 1, In my file there can be multiple \n in end of file. I want to remove it alltogether. – jn_sum Dec 26 '11 at 11:37
  • sed '/^[[:space:]]*$/{:a;$d;N;/\n[[:space:]]*$/ba}' filename | awk '{if (flag) print line; line = $0; flag = 1} END {printf line}' From the given script i am able to remove multiple \n from end of file :-) – jn_sum Dec 26 '11 at 12:33
  • 2
    post your comment as an answer and you can accept it, giving yourself some reputation points. (You have to wait X hours to get the points). In the future, learn how to use better tags to get more people looking at your question. (Roll you mouse over your tags and see the # of users, then see the counts for tag=awk tag=bash tag=sed). Good luck. – shellter Dec 26 '11 at 16:02
  • 1
    possible duplicate of [How can I delete a newline if it is the last character in a file?](http://stackoverflow.com/questions/1654021/how-can-i-delete-a-newline-if-it-is-the-last-character-in-a-file) – Luke Girvin Feb 11 '12 at 18:28

2 Answers2

13

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$ 
Aleksandra Zalcman
  • 3,352
  • 1
  • 19
  • 19
  • I am not able to remove \n from file, from the above script – jn_sum Dec 26 '11 at 11:36
  • 4
    Note, if `file1` is the same as `file2` it might erase the file. Be warned. Happened to me. Luckily I knew the contents. – Steven Lu Oct 17 '13 at 02:58
  • I would expect this not to work, at least not reliably, because `eof` only becomes true when you have tried to read *past the end* of the file, which hasn't necessarily happened in `-pe` mode when `$_` is the last line of the file. – zwol Dec 28 '13 at 05:56
4

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.)

zwol
  • 135,547
  • 38
  • 252
  • 361