windows 保留行尾
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4652652/
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
Preserve line endings
提问by Bogdan Calmac
I run sed to do some substitution on windows and I noticed that it automatically converts line endings to Unix (\n). Is there an option to tell sed to use Windows line endings (\r\n) or even better to preserve the line endings from the file?
我运行 sed 在 Windows 上做一些替换,我注意到它会自动将行尾转换为 Unix (\n)。是否可以选择告诉 sed 使用 Windows 行尾(\r\n),或者甚至更好地保留文件中的行尾?
Note: I use sed from unxutils: http://unxutils.sourceforge.net/
注意:我使用来自 unxutils 的 sed:http://unxutils.sourceforge.net/
回答by Shlomo
You can use the -b
option for sed to have it treat the file as binary. This will fix the problem with cygwin's sed on Windows.
您可以使用-b
sed 选项将文件视为二进制文件。这将解决 Windows 上 cygwin 的 sed 问题。
Example: sed -b 's/foo/bar/'
例子: sed -b 's/foo/bar/'
If you wish to match the end of the line, remember to match, capture and copy the optional carriage return.
如果你想匹配行尾,记得匹配、捕获和复制可选的回车。
Example: sed -b 's/foo\(\r\?\)$/bar\1/'
例子: sed -b 's/foo\(\r\?\)$/bar\1/'
From the sed man page:
从sed 手册页:
-b ?????--binary
This option is available on every platform, but is only effective where the operating system makes a distinction between text files and binary files. When such a distinction is made—as is the case for MS-DOS, Windows, Cygwin—text files are composed of lines separated by a carriage return and a line feed character, and sed does not see the ending CR. When this option is specified, sed will open input files in binary mode, thus not requesting this special processing and considering lines to end at a line feed.`
-b ?????--二进制
此选项在每个平台上都可用,但仅在操作系统区分文本文件和二进制文件时才有效。当进行这种区分时——如 MS-DOS、Windows、Cygwin 的情况——文本文件由由回车符和换行符分隔的行组成,并且 sed 看不到结尾的 CR。当指定此选项时,sed 将以二进制模式打开输入文件,因此不要求此特殊处理并考虑行以换行结束。`
回答by SiegeX
You could try to sub the \n
for \r\n
at the end of your existing script like so:
您可以尝试在现有脚本的末尾添加子\n
for \r\n
,如下所示:
sed 's/foo/bar/;s/$/\r/'
or perhaps
也许
sed -e 's/foo/bar/' -e 's/$/\r/'
If neither of the above two work, you'll have to consult the specific man page for your version of sed
to see if such an option exists. Note that the *nix versions of sed
do notalter the line terminators without being told to do so.
如果以上两个都不起作用,您必须查阅您的版本的特定手册页,sed
以查看是否存在这样的选项。注意的* nix的版本sed
都没有改变行终止,而不被告知这样做。
Another alternative is to use the cygwin
version of sed
which shouldn'thave this undesirable behavior.
另一种方法是使用cygwin
的版本,sed
它不应该有这种不良行为。
回答by seeker
Alternatively, (the cygwin version of) perl -pe
doesn't seem to have this problem.
或者,(cygwin 版本)perl -pe
似乎没有这个问题。
回答by buckley
Gnuwin can be suppressed to mess up the newlines (win->unix) if you only specify the -b switch and redirect. Using the -i (inline) switch will mess it up.
如果您仅指定 -b 开关和重定向,则可以抑制 Gnuwin 以弄乱换行符 (win->unix)。使用 -i(内联)开关会将其搞砸。
E.g. sed.exe -b "s/\xFF\xFE//" c:\temp\in.csv > c:\temp\out.csv
例如 sed.exe -b "s/\xFF\xFE//" c:\temp\in.csv > c:\temp\out.csv
回答by Vadzim
I've found that sed-4.4.exe
from https://github.com/mbuilov/sed-windowsis pure win as it
我发现sed-4.4.exe
从https://github.com/mbuilov/sed-windows是纯粹的胜利
- uses windows CRLF line endings in default mode
- preserves original line endings in
-b
mode - works correctly with in-place
-i
mode - also offers
-z
mode with\0
delimeters instead of\n
which may be handy sometimestoo
- 在默认模式下使用 windows CRLF 行尾
- 在
-b
模式下保留原始行尾 - 在就地
-i
模式下正常工作 - 还提供了
-z
模式\0
定界符,而不是\n
它可以很方便有时太
See also list of sed optionsand list of all windows sed ports.
Note that gnuwin32 sed 4.2.1does corrupt line endingsin -bi
mode and doesn't have -z
mode at all.
请注意,gnuwin32 sed 4.2.1会在mode中损坏行尾-bi
并且根本没有-z
mode。