如何检查文本文件的行尾以查看它是unix还是dos格式? [英] how to check end-of-line of a text file to see if it is unix or dos format?
本文介绍了如何检查文本文件的行尾以查看它是unix还是dos格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要将文本文件转换为dos格式(每行以0x0d0x0a
结尾,而不是仅0x0a
结尾),如果该文件为Unix格式(0x0a
仅在每行末尾).
I need to convert the text file to dos format (ending each line with 0x0d0x0a
, rather than 0x0a
only), if the file is in unix format (0x0a
only at the end of each line).
我知道如何转换(sed 's/$/^M/'
),但不知道如何检测文件的行尾字符.
I know how to convert it (sed 's/$/^M/'
), but don't how how to detect the end-of-line character(s) of a file.
我正在使用ksh.
任何帮助将不胜感激.
[更新]: 有点想通了,这是我的ksh脚本来进行检查.
[Update]: Kind of figured it out, and here is my ksh script to do the check.
[qiangxu@host:/my/folder]# cat eol_check.ksh
#!/usr/bin/ksh
if ! head -1 $1 |grep ^M$ >/dev/null 2>&1; then
echo UNIX
else
echo DOS
fi
在上面的脚本中,应将^M
与Ctrl-V
和Ctrl-M
一起插入vi
.
In the above script, ^M
should be inserted in vi
with Ctrl-V
and Ctrl-M
.
想知道是否有更好的方法.
Want to know if there is any better method.
推荐答案
if awk '/\r$/{exit 0;} 1{exit 1;}' myFile
then
echo "is DOS"
fi
这篇关于如何检查文本文件的行尾以查看它是unix还是dos格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文