我有一堆(数百个)应该有Unix行结尾的文件.我强烈怀疑其中一些有Windows行结尾,我想以编程方式找出哪些行.
我知道我可以跑了
flip -u或者在脚本中类似的东西来转换所有东西,但我希望能够识别那些需要先改变的文件.
我有两个文件,一个有换行,一个没有:
文件:text_without_newline
$root@kali:/home#cat text_without_empty_line
This is a Testfile
This file does not contain an empty line at the end
$root@kali:/home#
Run Code Online (Sandbox Code Playgroud)
文件:text_with_newline
$root@kali:/home#cat text_with_empty_line
This is a Testfile
This file does contain an empty line at the end
$root@kali:/home#
Run Code Online (Sandbox Code Playgroud)
是否有命令或函数来检查文件末尾是否有换行符?我已经找到了这个解决方案,但它对我不起作用.(编辑:IGNORE:使用preg_match和PHP的解决方案也可以.)
我想在正在提交的文本文件上运行以下脚本:
# Send the commands H and w to ed
# ed will append newline if the file does not end in one
printf "%s\n" H w | ed -s $1
# Strip trailing whitespace
sed -i 's/[ \t]*$//g' $1
# Convert tabs to 4 spaces
sed -i -r "s/\t/ /g" $1
Run Code Online (Sandbox Code Playgroud)
我看到subversion有一个start-commit和pre-commit钩子,但是我不能按照有关如何用上面的脚本处理文本文件的文档.