如何检测文件以换行符结尾? [英] How to detect file ends in newline?

查看:23
本文介绍了如何检测文件以换行符结尾?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Over at 提交 subversion 时可以修改文本文件吗? 格兰特建议我阻止提交.

Over at Can you modify text files when committing to subversion? Grant suggested that I block commits instead.

但是我不知道如何检查文件是否以换行符结尾.如何检测文件是否以换行符结尾?

However I don't know how to check a file ends with a newline. How can you detect that the file ends with a newline?

推荐答案

@Konrad:tail 不返回空行.我制作了一个文件,其中包含一些不以换行符结尾的文本和一个以换行符结尾的文件.这是tail的输出:

@Konrad: tail does not return an empty line. I made a file that has some text that doesn't end in newline and a file that does. Here is the output from tail:

$ cat test_no_newline.txt
this file doesn't end in newline$ 

$ cat test_with_newline.txt
this file ends in newline
$

虽然我发现 tail 有获取最后一个字节的选项.所以我将你的脚本修改为:

Though I found that tail has get last byte option. So I modified your script to:

#!/bin/sh
c=`tail -c 1 $1`
if [ "$c" != "" ]; then
    echo "no newline"
fi

这篇关于如何检测文件以换行符结尾?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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