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

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


当前回答

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

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

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

其他回答

在...

:设置列表以查看行结束符。

:将nolist设置为正常。

虽然我不认为你可以在vi中看到\n或\r\n,但你可以看到它是哪种类型的文件(UNIX, DOS等)来推断它有哪些行尾…

:设置FF

或者,在bash中可以使用od -t c <filename>或od -c <filename>来显示返回值。

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$。

在bash shell中,尝试cat -v <filename>。这将显示windows文件的回车。

(这适用于我在Windows XP上通过Cygwin的rxvt)。

编者注:cat -v显示\r (CR)字符。^ M。因此,行将结束的\r\n序列将在每一行的末尾显示为^M。Cat -e将额外可视化\n,即$。(cat -et将额外可视化制表符字符。^我。)

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