Answers:
您可以使用该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
转换已转换的文件没有任何效果,因此可以安全地盲目运行(即,无需先测试格式),尽管通常适用免责声明。
tofrodos软件包时,sudo apt-get install tofrodos就像-必须运行sudo apt-get install dos2unixget dos2unix和一样unix2dos。
dos2unix失败了?该问题的OP仅模糊地描述了该问题。
在vi...
:set list 查看行尾。
:set nolist 恢复正常。
虽然我认为您看不到\n或\r\n中的内容vi,但您可以看到它是哪种文件类型(UNIX,DOS等),以推断它具有哪些行尾。
:set ff
或者,bash您可以使用od -t c <filename>或仅仅od -c <filename>显示退货。
:set fileformat将报告文件的行尾在哪个unix或dosvim中。您可以通过进行更改:set fileformat=unix。
od -t c file/path,但是感谢您的新程序。很棒!
在bash外壳中,尝试cat -v <filename>。这应该显示Windows文件的回车符。
(这在Windows XP上通过Cygwin在rxvt中为我工作)。
编者注:cat -v可视化\r(CR)字符。作为^M。因此,行结束\r\n序列将显示^M在每条输出行的末尾。cat -e还将可视化\n,即$。(cat -et还将可视化制表符^I。)。
echo -e 'abc\ndef\r\n' | cat -v,您应该^M在“ def”之后看到a 。
要显示CR ^M较少使用或少less -u键入-u一次,请打开。
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.
file然后file -k再dos2unix -ihfile通常就足够了。但是对于困难的情况,请尝试file -k或dosunix -ih。
详细信息如下。
file -k简短版: file -k somefile.txt会告诉您。
with CRLF line endings为DOS / Windows行尾输出。with LF line endingsMAC线尾。text。(因此,如果未明确提及任何种类的内容,line endings则表示其含义为:“ CR线末端”。)长版请参见下文。
有时我需要检查一下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)行结尾。(其他文件的末尾LF是Unix()。在此输出中不是显式的。它是隐式的。这只是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我在编写上面的示例时不知道这一点,但是:
其实事实证明,DOS2UNIX的会给你一个标题行,如果你使用-ih(简称--info=h),像这样:
$ 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
还有另一个“实际”时刻:标头格式确实很容易记住:这是两个助记符:
man fileman dos2unixAccounts.java: Java source, ASCII text\012-在MinTTY的Windows上
file -k Accounts.java也在git-for-windows附带的薄荷糖中尝试过,但我的版本是git version 2.21.0.windows.1
cat -e file_to_test
您可以xxd用来显示文件的十六进制转储,并搜索“ 0d0a”或“ 0a”字符。
您可以cat -v <filename>按照@warriorpostman的建议使用。
您可以用来vim -b filename以二进制模式编辑文件,该文件将显示^ M个字符以表示回车,并且换行表示LF存在,表示Windows CRLF行尾。LF是我的意思\n,CR是我的意思\r。请注意,当您使用-b选项时,默认情况下始终会在UNIX模式下编辑文件,如[unix]状态行所指示,这意味着,如果添加新行,它们将以LF而不是CRLF结尾。如果在带有CRLF行尾的文件上使用不带-b的普通vim,则应该[dos]在状态行中看到显示,并且插入的行将CRLF作为行尾。用于fileformats设置的vim文档说明了复杂性。
另外,我没有足够的要点来注释Notepad ++的答案,但是如果您在Windows上使用Notepad ++,请使用“查看/显示符号/显示行尾”菜单来显示CR和LF。在这种情况下,显示了LF,而对于vim,LF用新的线表示。
^M如果您希望始终在vim render中看到Windows换行符^M,则可以将此行添加到您的.vimrc:
set ffs=unix
这将使vim将您打开的每个文件解释为一个unix文件。由于unix文件具有\n换行符,因此,具有换行符的Windows文件\r\n仍将正确呈现(由于\n),但将^M在文件末尾呈现(这是vim呈现\r字符的方式)。
如果只希望基于每个文件进行设置,则可以:e ++ff=unix在编辑给定文件时使用。
unixvs dos)如果你想让Vim的底线,始终显示的内容对文件类型你是编辑(和你没有力设定的文件类型为UNIX),您可以添加到您的statusline使用
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
在文件的底部
unixvs dos)如果您只想查看文件的类型,则可以使用:set fileformat(如果您已强制设置文件类型,则无法使用)。unix对于UNIX文件和dosWindows ,它将返回。
man less。