我试图使用bash中的一些东西向我显示打印而不是解释的文件中的行结束符。该文件是从SSIS/SQL Server中转储的,由Linux机器读入以进行处理。

在vi中有任何开关,少,多,等等? 除了看到行结束符,我还需要知道它是什么类型的行结束符(CRLF或LF)。我怎么知道呢?


当前回答

我将输出转储到一个文本文件中。然后我在notepad++中打开它,然后单击显示所有字符按钮。不是很优雅,但是很好用。

其他回答

您可以使用xxd显示文件的十六进制转储,并查找“0d0a”或“0a”字符。

你可以使用cat -v <filename>,就像@warriorpostman建议的那样。

You can use vim -b filename to edit a file in binary mode, which will show ^M characters for carriage return and a new line is indicative of LF being present, indicating Windows CRLF line endings. By LF I mean \n and by CR I mean \r. Note that when you use the -b option the file will always be edited in UNIX mode by default as indicated by [unix] in the status line, meaning that if you add new lines they will end with LF, not CRLF. If you use normal vim without -b on a file with CRLF line endings, you should see [dos] shown in the status line and inserted lines will have CRLF as end of line. The vim documentation for fileformats setting explains the complexities.

另外,我没有足够的点来评论notepad++的答案,但如果你在Windows上使用notepad++,使用查看/显示符号/显示行结束菜单来显示CR和LF。在本例中显示LF,而对于vim, LF由新行表示。

Ubuntu 14.04:

简单的cat -e <filename>就可以了。

这将Unix的行结束符(\n或LF)显示为$,Windows的行结束符(\r\n或CRLF)显示为^M$。

可以使用命令todos filename转换为DOS结尾,使用命令fromdos filename转换为UNIX行结尾。要在Ubuntu上安装软件包,输入sudo apt-get install tofrodos。

您可以使用文件实用程序来指示行结束符的类型。

Unix:

$ file testfile1.txt
testfile.txt: ASCII text

“DOS”:

$ file testfile2.txt
testfile2.txt: ASCII text, with CRLF line terminators

将“DOS”转换为Unix:

$ dos2unix testfile2.txt

从Unix转换到DOS:

$ unix2dos testfile1.txt

转换一个已经转换的文件没有效果,所以盲目运行是安全的(即不先测试格式),尽管通常的免责声明适用,一如既往。