如何删除换行符,如果它是在一个文件中的最后一个字符? [英] How can I delete a newline if it is the last character in a file?

查看:1000
本文介绍了如何删除换行符,如果它是在一个文件中的最后一个字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些我想删除最后一个换行符,如果它是在一个文件中的最后一个字符的文件。 'OD -c'显示我,我运行的命令不写一个尾随新行的文件:

  0013600ñT> \\ n

我试着SED,但我能想到的最好的是不是做的伎俩一些小技巧:

  SED -e'$ S / \\(。* \\)\\ N $ / \\ 1 /'ABC

任何想法如何做到这一点?


解决方案

 的perl -pe'格格如果EOF'的文件名和GT;文件名2

,或编辑文件的地方:

 的perl -pi -e'格格如果EOF文件名

[编者注: -pi -e 原是 -pie ,但是,正如所指出一些评论和解释@hvd,后者不能正常工作。]

这被形容为awk的网站,我看到一个perl的亵渎。

不过,在测试中,它的工作。

I have some files that I'd like to delete the last newline if it is the last character in a file. 'od -c' shows me that the command I run does write the file with a trailing new line:

0013600   n   t  >  \n

I've tried a few tricks with sed but the best I could think of isn't doing the trick:

sed -e '$s/\(.*\)\n$/\1/' abc

Any ideas how to do this?

解决方案

perl -pe 'chomp if eof' filename >filename2

or, to edit the file in place:

perl -pi -e 'chomp if eof' filename

[Editor's note: -pi -e was originally -pie, but, as noted by several commenters and explained by @hvd, the latter doesn't work.]

This was described as a 'perl blasphemy' on the awk website I saw.

But, in a test, it worked.

这篇关于如何删除换行符,如果它是在一个文件中的最后一个字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆