391

我试图在 bash 中使用一些东西来向我展示打印而不是解释的文件中的行尾。该文件是来自 SSIS/SQL Server 的转储文件,正在被 Linux 机器读取以进行处理。

  • vi, less,more等内是否有任何开关?

  • 除了查看行尾,我还需要知道它是什么类型的行尾(CRLFLF)。我怎么知道呢?

4

11 回答 11

509

您可以使用该file实用程序来指示行尾的类型。

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

转换已转换的文件没有任何效果,因此可以安全地盲目运行(即无需先测试格式),尽管通常的免责声明一如既往地适用。

于 2010-08-25T22:00:50.853 回答
205

Ubuntu 14.04:

简单的cat -e <filename>作品就好了。

这会将 Unix 行尾(\n或 LF)显示为$,将 Windows 行尾(\r\n或 CRLF)显示为^M$

于 2015-12-20T00:49:00.647 回答
139

vi...

:set list查看行尾。

:set nolist恢复正常。

虽然我认为您看不到\n\r\nin vi,但您可以看到它是哪种类型的文件(UNIX、DOS 等)来推断它具有哪些行尾...

:set ff

或者,bash您可以使用od -t c <filename>或仅od -c <filename>显示返回。

于 2010-08-25T20:42:38.730 回答
111

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

(这在 Windows XP 上通过 Cygwin 在 rxvt 中对我有用)。

编者注:cat -v可视化\r(CR)字符。作为^M. 因此,行尾\r\n序列将显示为^M每个输出行的末尾。cat -e将另外可视化\n,即 as $。(cat -et将另外可视化标签字符。作为^I。)

于 2010-08-25T21:01:31.057 回答
29

试试file,然后file -k,然后dos2unix -ih

file通常就足够了。但对于棘手的情况,请尝试file -kor dosunix -ih

详情如下。


尝试file -k

简短版: file -k somefile.txt会告诉你。

  • 它将输出with CRLF line endingsDOS/Windows 行尾。
  • 它将输出with CR line endingsMAC 行结尾。
  • 对于 Linux/Unix 行“LF”,它只会输出text. (因此,如果它没有明确提及任何类型,line endings那么这隐含的意思是:“LF 行结尾”。)

长版见下文。


真实世界示例:证书编码

我有时必须检查 PEM 证书文件。

常规的问题file在于:有时它试图太聪明/太具体。

让我们做一个小测验:我有一些文件。其中一个文件具有不同的行尾。哪一个?

(顺便说一句:这是我典型的“证书工作”目录之一。)

让我们尝试常规file

$ file -- *
0.example.end.cer:         PEM certificate
0.example.end.key:         PEM RSA private key
1.example.int.cer:         PEM certificate
2.example.root.cer:        PEM certificate
example.opensslconfig.ini: ASCII text
example.req:               PEM certificate request

嗯。它没有告诉我行尾。而且我已经知道那些是证书文件。我不需要“文件”来告诉我。

你还能尝试什么?

您可以尝试dos2unix使用这样的--info开关:

$ dos2unix --info -- *
  37       0       0  no_bom    text    0.example.end.cer
   0      27       0  no_bom    text    0.example.end.key
   0      28       0  no_bom    text    1.example.int.cer
   0      25       0  no_bom    text    2.example.root.cer
   0      35       0  no_bom    text    example.opensslconfig.ini
   0      19       0  no_bom    text    example.req

所以这告诉你:是的,“0.example.end.cer”一定是个奇怪的人。但是有什么样的行尾呢?你知道dos2unix的输出格式吗(我不。)

但幸运的是,有--keep-going(或-k简称)选项file

$ file --keep-going -- *
0.example.end.cer:         PEM certificate\012- , ASCII text, with CRLF line terminators\012- data
0.example.end.key:         PEM RSA private key\012- , ASCII text\012- data
1.example.int.cer:         PEM certificate\012- , ASCII text\012- data
2.example.root.cer:        PEM certificate\012- , ASCII text\012- data
example.opensslconfig.ini: ASCII text\012- data
example.req:               PEM certificate request\012- , ASCII text\012- data

优秀的!现在我们知道我们的奇数文件有 DOS ( CRLF) 行结尾。(并且其他文件具有 Unix ( LF) 行结尾。这在此输出中不明确。它是隐含的。这只是file期望“常规”文本文件的方式。)

(如果你想分享我的助记符:“L”代表“Linux”和“LF”。)

现在让我们转换罪魁祸首再试一次:

$ dos2unix -- 0.example.end.cer

$ file --keep-going -- *
0.example.end.cer:         PEM certificate\012- , ASCII text\012- data
0.example.end.key:         PEM RSA private key\012- , ASCII text\012- data
1.example.int.cer:         PEM certificate\012- , ASCII text\012- data
2.example.root.cer:        PEM certificate\012- , ASCII text\012- data
example.opensslconfig.ini: ASCII text\012- data
example.req:               PEM certificate request\012- , ASCII text\012- data  

好的。现在所有证书都有 Unix 行结尾。

尝试dos2unix -ih

我在写上面的例子时不知道这一点,但是:

实际上,如果你像这样使用-ih(缩写--info=h),dos2unix 会给你一个标题行:

$ dos2unix -ih -- *
 DOS    UNIX     MAC  BOM       TXTBIN  FILE
   0      37       0  no_bom    text    0.example.end.cer
   0      27       0  no_bom    text    0.example.end.key
   0      28       0  no_bom    text    1.example.int.cer
   0      25       0  no_bom    text    2.example.root.cer
   0      35       0  no_bom    text    example.opensslconfig.ini
   0      19       0  no_bom    text    example.req

另一个“实际”时刻:标题格式真的很容易记住:这里有两个助记符:

  1. 它是 DUMB(从左到右:d 代表 Dos,u 代表 Unix,m 代表 Mac,b 代表 BOM)。
  2. 而且:“DUM”只是 D、U 和 M 的字母顺序。

进一步阅读

于 2017-11-22T13:19:24.250 回答
21

将 CR 显示为^M较少使用less -u或键入-u一次 less 已打开。

man less说:

-u or --underline-special

      Causes backspaces and carriage returns to be treated  as  print-
      able  characters;  that  is,  they are sent to the terminal when
      they appear in the input.
于 2015-07-27T15:03:35.150 回答
9

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

您可以cat -v <filename>按照@warriorpostman 的建议使用。

于 2013-09-10T16:50:08.593 回答
5

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

于 2012-10-28T22:13:06.023 回答
5

您可以使用vim -b filename二进制模式编辑文件,这将显示 ^M 字符作为回车符,并且新行表示存在 LF,表示 Windows CRLF 行结尾。我的意思是 LF,我的意思\n是 CR \r。请注意,当您使用 -b 选项时,默认情况下文件将始终在 UNIX 模式下编辑,如[unix]状态行中所示,这意味着如果您添加新行,它们将以 LF 结尾,而不是 CRLF。如果您在带有 CRLF 行结尾的文件上使用不带 -b 的普通 vim,您应该[dos]会在状态行中看到显示,并且插入的行将以 CRLF 作为行尾。用于设置的 vim 文档fileformats解释了复杂性。

另外,我没有足够的分数来评论 Notepad++ 的答案,但是如果您在 Windows 上使用 Notepad++,请使用 View / Show Symbol / Show End of Line 菜单来显示 CR 和 LF。在这种情况下,显示的是 LF,而对于 vim,LF 由一个新行表示。

于 2017-09-15T06:01:28.300 回答
1

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

于 2015-10-13T18:56:52.283 回答
0

Vim - 总是将 Windows 换行显示为^M

如果您希望始终在 vim 中看到 Windows 换行符^M,则可以将此行添加到您的.vimrc:

set ffs=unix

这将使 vim 将您打开的每个文件解释为 unix 文件。由于 unix 文件具有\n换行符,带有换行符的 windows 文件\r\n仍将正确呈现(感谢\n),但将^M在文件末尾具有(vim 呈现\r字符的方式)。


Vim - 有时显示 Windows 换行符

如果您只想在每个文件的基础上设置它,您可以:e ++ff=unix在编辑给定文件时使用。


Vim - 总是显示文件类型 ( unixvs dos)

如果您希望 vim 的底线始终显示您正在编辑的文件类型(并且您没有强制将文件类型设置为 unix),您可以添加到您的statuslinewith
set statusline+=\ %{&fileencoding?&fileencoding:&encoding}.

下面提供了我的完整状态栏。只需将其添加到您的.vimrc.

" Make statusline stay, otherwise alerts will hide it
set laststatus=2
set statusline=
set statusline+=%#PmenuSel#
set statusline+=%#LineNr#
" This says 'show filename and parent dir'
set statusline+=%{expand('%:p:h:t')}/%t
" This says 'show filename as would be read from the cwd'
" set statusline+=\ %f
set statusline+=%m\
set statusline+=%=
set statusline+=%#CursorColumn#
set statusline+=\ %y
set statusline+=\ %{&fileencoding?&fileencoding:&encoding}
set statusline+=\[%{&fileformat}\]
set statusline+=\ %p%%
set statusline+=\ %l:%c
set statusline+=\ 

它会像

.vim/vimrc\                                    [vim] utf-8[unix] 77% 315:6

在文件的底部


Vim - 有时显示文件类型 ( unixvs dos)

如果您只想查看您拥有的文件类型,可以使用:set fileformat(如果您强制设置文件类型,这将不起作用)。unix对于 unix 文件和dosWindows ,它将返回。

于 2019-11-19T19:40:03.130 回答