Linux 如何找出文本文件中的行尾?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3569997/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to find out line-endings in a text file?
提问by Marco Ceppi
I'm trying to use something in bash to show me the line endings in a file printed rather than interpreted. The file is a dump from SSIS/SQL Server being read in by a Linux machine for processing.
我试图在 bash 中使用一些东西来向我显示打印而不是解释的文件中的行尾。该文件是来自 SSIS/SQL Server 的转储,由 Linux 机器读入以进行处理。
Are there any switches within
vi
,less
,more
, etc?In addition to seeing the line-endings, I need to know what type of line end it is (
CRLF
orLF
). How do I find that out?
是否有内的任何开关
vi
,less
,more
,等?除了查看行尾之外,我还需要知道它是什么类型的行尾(
CRLF
或LF
)。我怎么知道?
采纳答案by Paused until further notice.
You can use the file
utility to give you an indication of the type of line endings.
您可以使用该file
实用程序来指示行结尾的类型。
Unix:
Unix:
$ file testfile1.txt
testfile.txt: ASCII text
"DOS":
“DOS”:
$ file testfile2.txt
testfile2.txt: ASCII text, with CRLF line terminators
To convert from "DOS" to Unix:
要将“DOS”转换为 Unix:
$ dos2unix testfile2.txt
To convert from Unix to "DOS":
从 Unix 转换为“DOS”:
$ unix2dos testfile1.txt
Converting an already converted file has no effect so it's safe to run blindly (i.e. without testing the format first) although the usual disclaimers apply, as always.
转换已转换的文件没有任何影响,因此可以安全地盲目运行(即不首先测试格式),尽管一如既往地适用通常的免责声明。
回答by Ryan Berger
In vi
...
在vi
...
:set list
to see line-endings.
:set list
查看行尾。
:set nolist
to go back to normal.
:set nolist
恢复正常。
While I don't think you can see \n
or \r\n
in vi
, you can see which type of file it is (UNIX, DOS, etc.) to infer which line endings it has...
虽然我认为您看不到\n
或\r\n
in vi
,但您可以看到它是哪种类型的文件(UNIX、DOS 等)以推断它的行尾...
:set ff
:set ff
Alternatively, from bash
you can use od -t c <filename>
or just od -c <filename>
to display the returns.
或者,bash
您可以使用od -t c <filename>
或 仅od -c <filename>
显示回报。
回答by warriorpostman
In the bash shell, try cat -v <filename>
. This should display carriage-returns for windows files.
在 bash shell 中,尝试cat -v <filename>
. 这应该显示 Windows 文件的回车符。
(This worked for me in rxvt via Cygwin on Windows XP).
(这在 rxvt 中通过 Windows XP 上的 Cygwin 对我有用)。
Editor's note: cat -v
visualizes \r
(CR) chars. as ^M
. Thus, line-ending \r\n
sequences will display as ^M
at the end of each output line. cat -e
will additionally visualize \n
, namely as $
. (cat -et
will additionally visualize tab chars. as ^I
.)
编者注:cat -v
可视化\r
(CR)字符。作为^M
。因此,行结束\r\n
序列将显示^M
在每个输出行的末尾。cat -e
将另外可视化\n
,即 as $
。(cat -et
另外将制表符可视化。如^I
。)
回答by Zorayr
You may use the command todos filename
to convert to DOS endings, and fromdos filename
to convert to UNIX line endings. To install the package on Ubuntu, type sudo apt-get install tofrodos
.
您可以使用该命令todos filename
转换为 DOS 结尾,也fromdos filename
可以转换为 UNIX 行结尾。要在 Ubuntu 上安装软件包,请键入sudo apt-get install tofrodos
.
回答by Rich
You can use xxd
to show a hex dump of the file, and hunt through for "0d0a" or "0a" chars.
您可以使用xxd
显示文件的十六进制转储,并搜索“0d0a”或“0a”字符。
You can use cat -v <filename>
as @warriorpostman suggests.
您可以cat -v <filename>
按照@warriorpostman 的建议使用。
回答by P. Kucerak
To show CR as ^M
in less use less -u
or type -uonce less is open.
将 CR 显示为^M
较少使用less -u
或-u打开一次较少使用。
man less
says:
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.
-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.
回答by Diego
I dump my output to a text file. I then open it in notepad ++ then click the show all characters button. Not very elegant but it works.
我将输出转储到文本文件。然后我在记事本++中打开它,然后单击显示所有字符按钮。不是很优雅,但它的工作原理。
回答by Alexander Shelemin
Ubuntu 14.04:
Ubuntu 14.04:
simple cat -e <filename>
works just fine.
简单的cat -e <filename>
作品就好了。
This displays Unix line endings (\n
or LF) as $
and Windows line endings (\r\n
or CRLF) as ^M$
.
这将 Unix 行结尾(\n
或 LF)显示为$
,Windows 行结尾(\r\n
或 CRLF)显示为^M$
。
回答by smalers
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.
您可以使用vim -b filename
二进制模式编辑文件,它将显示 ^M 字符作为回车符,新行表示存在 LF,表示 Windows CRLF 行结束。我的意思是 LF,我的意思\n
是 CR \r
。请注意,当您使用 -b 选项时,默认情况下,文件将始终在 UNIX 模式下编辑,如[unix]
状态行中所示,这意味着如果您添加新行,它们将以 LF 而不是 CRLF 结尾。如果您在带有 CRLF 行尾的文件上使用没有 -b 的普通 vim,您应该会[dos]
在状态行中看到显示,并且插入的行将以 CRLF 作为行尾。fileformats
设置的 vim 文档解释了复杂性。
Also, I don't have enough points to comment on the Notepad++ answer, but if you use Notepad++ on Windows, use the View / Show Symbol / Show End of Line menu to display CR and LF. In this case LF is shown whereas for vim the LF is indicated by a new line.
另外,我没有足够的点数来评论 Notepad++ 的答案,但是如果您在 Windows 上使用 Notepad++,请使用“查看”/“显示符号”/“显示行尾”菜单来显示 CR 和 LF。在这种情况下显示 LF,而对于 vim,LF 由一个新行表示。
回答by StackzOfZtuff
Try "file -k"
尝试“文件-k”
Short version:file -k somefile.txt
will tell you.
简短版本:file -k somefile.txt
会告诉你。
- It will output
with CRLF line endings
for DOS/Windows line endings. - It will output
with LF line endings
for MAC line endings. - And for Linux/Unix line "CR" it will just output
text
. (So if it does not explicitly mention any kind ofline endings
then this implicitly means: "CR line endings".)
- 它将输出
with CRLF line endings
DOS/Windows 行结尾。 - 它将输出
with LF line endings
MAC 行结尾。 - 对于 Linux/Unix 行“CR”,它只会输出
text
. (因此,如果它没有明确提及任何种类,line endings
那么这隐含地意味着:“CR 行结尾”。)
Long versionsee below.
长版见下文。
Real world application: Certificate Encoding
实际应用:证书编码
I sometimes have to check this for PEM certificate files.
我有时必须检查 PEM 证书文件。
The trouble with regular file
is this: Sometimes it's trying to be too smart/too specific.
常规的问题file
在于:有时它试图过于聪明/过于具体。
Let's try a little quiz: I've got some files. And one of these files has different line endings. Which one?
让我们来做一个小测验:我有一些文件。其中一个文件具有不同的行尾。哪一个?
(By the way: this is what one of my typical "certificate work" directories looks like.)
(顺便说一句:这是我典型的“证书工作”目录之一。)
Let's try regular 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
Huh. It's not telling me the line endings. And I already knewthat those were cert files. I didn't need "file" to tell me that.
呵呵。它没有告诉我行尾。我已经知道那些是证书文件。我不需要“文件”来告诉我。
What else can you try?
你还能尝试什么?
You might try dos2unix
with the --info
switch like this:
您可以尝试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
So that tells you that: yup, "0.example.end.cer" must be the odd man out. But what kind of line endings are there? Do youknow the dos2unix output format by heart? (I don't.)
所以这告诉你:是的,“0.example.end.cer”一定是个奇怪的人。但是有什么样的行尾呢?难道你知道通过心脏的DOS2UNIX的输出格式?(我不。)
But fortunately there's the --keep-going
(or -k
for short) option in file
:
但幸运的是有--keep-going
(或-k
简称)选项file
:
$ file --keep-going -- *
0.example.end.cer: PEM certificate2- , ASCII text, with CRLF line terminators2- data
0.example.end.key: PEM RSA private key2- , ASCII text2- data
1.example.int.cer: PEM certificate2- , ASCII text2- data
2.example.root.cer: PEM certificate2- , ASCII text2- data
example.opensslconfig.ini: ASCII text2- data
example.req: PEM certificate request2- , ASCII text2- data
Excellent! Now we know that our odd file has DOS (CRLF
) line endings. (And the other files have Unix (LF
) line endings. This is not explicit in this output. It's implicit. It's just the way file
expects a "regular" text file to be.)
优秀!现在我们知道我们的奇数文件有 DOS ( CRLF
) 行结尾。(而其他文件有 Unix ( LF
) 行结尾。这在这个输出中不是明确的。它是隐含的。这只是file
期望“常规”文本文件的方式。)
(If you wanna share my mnemonic: "L" is for "Linux" and for "LF".)
(如果你想分享我的助记符:“L”代表“Linux”和“LF”。)
Now let's convert the culprit and try again:
现在让我们转换罪魁祸首并重试:
$ dos2unix -- 0.example.end.cer
$ file --keep-going -- *
0.example.end.cer: PEM certificate2- , ASCII text2- data
0.example.end.key: PEM RSA private key2- , ASCII text2- data
1.example.int.cer: PEM certificate2- , ASCII text2- data
2.example.root.cer: PEM certificate2- , ASCII text2- data
example.opensslconfig.ini: ASCII text2- data
example.req: PEM certificate request2- , ASCII text2- data
Good. Now all certs have Unix line endings.
好的。现在所有证书都有 Unix 行结尾。
Further reading
进一步阅读
man file
man dos2unix
- Wikipedia: Newline