我试图使用bash中的一些东西向我显示打印而不是解释的文件中的行结束符。该文件是从SSIS/SQL Server中转储的,由Linux机器读入以进行处理。
在vi中有任何开关,少,多,等等? 除了看到行结束符,我还需要知道它是什么类型的行结束符(CRLF或LF)。我怎么知道呢?
我试图使用bash中的一些东西向我显示打印而不是解释的文件中的行结束符。该文件是从SSIS/SQL Server中转储的,由Linux机器读入以进行处理。
在vi中有任何开关,少,多,等等? 除了看到行结束符,我还需要知道它是什么类型的行结束符(CRLF或LF)。我怎么知道呢?
当前回答
可以使用命令todos filename转换为DOS结尾,使用命令fromdos filename转换为UNIX行结尾。要在Ubuntu上安装软件包,输入sudo apt-get install tofrodos。
其他回答
在bash shell中,尝试cat -v <filename>。这将显示windows文件的回车。
(这适用于我在Windows XP上通过Cygwin的rxvt)。
编者注:cat -v显示\r (CR)字符。^ M。因此,行将结束的\r\n序列将在每一行的末尾显示为^M。Cat -e将额外可视化\n,即$。(cat -et将额外可视化制表符字符。^我。)
要在less中将CR显示为^M,请使用less -u或在less打开时键入-u。
Man less说:
-u或——下划线-特殊 导致退格和回车被视为打印 能力特征;也就是说,它们被发送到终端时 它们出现在输入中。
我将输出转储到一个文本文件中。然后我在notepad++中打开它,然后单击显示所有字符按钮。不是很优雅,但是很好用。
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由新行表示。
您可以使用文件实用程序来指示行结束符的类型。
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
转换一个已经转换的文件没有效果,所以盲目运行是安全的(即不先测试格式),尽管通常的免责声明适用,一如既往。