简体   繁体   English

如何在Windows上防止\\ n转换为\\ r \\ n

[英]How to prevent \n to from being translated to \r\n on Windows

I am using Windows 10 and Strawberry Perl 我正在使用Windows 10和Strawberry Perl

It is well know that the line terminator in Linux is \\n , and in Windows is \\r\\n . 众所周知,Linux中的行终止符为\\n ,而Windows中的行终止符为\\r\\n

I found that, on my computer, files of Linux type will automatically transform to windows type \\r\\n after a replacement operation like 我发现,在我的计算机上,执行类似以下的替换操作后,Linux类型的文件将自动转换为Windows类型\\r\\n

perl -i.bak -pe "s/aaa/bbb/g" test.txt

But this is not what I want, and it seems unreasonable. 但这不是我想要的,而且似乎不合理。 I would like to know if this is a Strawberry Perl issue, or another factor? 我想知道这是Strawberry Perl的问题还是其他因素?

How can I leave the line terminator unaffected on Windows? 如何使行终止符在Windows上不受影响?

This is standard behavior of Perl on Windows (to convert \\n to \\r\\n ). 这是Windows上Perl的标准行为(将\\n转换为\\r\\n )。

You can get around it by using binmode , which prevents Perl from doing the automatic line-ending conversion. 您可以使用binmode解决该binmode ,它可以防止Perl执行自动行尾转换。

Your command would then be changed to look like this. 然后,您的命令将更改为如下所示。 It tells binmode to write to STDOUT and then that output has to be redirected to another file. 它告诉binmode写入STDOUT ,然后必须将输出重定向到另一个文件。 The following command should do what you want (though not in place): 以下命令应该执行您想要的操作(尽管不到位):

perl -pe "BEGIN{ binmode(STDOUT) } s/aaa/bbb/g" test.txt > newtest.txt

"Actually I set unix format as notepad++ default which is my main editor" I think you should make the effort the keep files with the correct line endings for the appropriate system. “实际上,我将unix格式设置为notepad ++默认值,这是我的主编辑器”,我认为您应该努力为相应的系统保留具有正确行尾的文件。 You won't make any friends if you keep Linux files everywhere, as it will make it very hard for others to work with your non-standard methodology 如果到处都保留Linux文件,您将不会结交任何朋友,因为这会使其他人很难使用您的非标准方法

It isn't very hard to work with both systems properly, as all you have to do is to make the change automatically when copying from one system to another. 正确使用两个系统并不是很难,因为从一个系统复制到另一个系统时,您要做的就是自动进行更改。 You can use dos2unix and unix2dos when making the copy, but it would be a simple job to write a Perl program to update all of your systems with the relevant version of the text files 制作副本时可以使用dos2unixunix2dos ,但是编写Perl程序以使用文本文件的相关版本更新所有系统将是一项简单的工作。

However, if you insist on this plan, this should help you to achieve it 但是,如果您坚持这个计划,这应该可以帮助您实现它

By default, when running on Windows, perl will use the IO layers :unix and :crlf , which means it works the same as on a Linux system but will translate CRLF to LF on input, and LF to CRLF on output 默认情况下,当在Windows上运行时,perl将使用IO层:unix:crlf ,这意味着它与Linux系统上的工作原理相同,但是在输入时将CRLF转换为LF,在输出时将LF转换为CRLF

You can make individual open calls behave differently by adding an explicit pseudo-layer :raw , which removes the :crlf layer. 您可以通过添加一个显式伪层:raw来删除单个:crlf层,从而使单独的open调用行为有所不同。 But if you want to modify the special file handles STDIN , STDOUT and ARGV then you need a different tactic, because those handles are opened for you by perl 但是,如果要修改特殊文件句柄STDINSTDOUTARGV则需要采取不同的策略,因为这些句柄是由perl为您打开的

You can use the open pragma at the top of your program, like this 您可以使用程序顶部的open编译指示,如下所示

use open IO => ':raw';

which will implicitly apply the :raw layer to every input or output file handle, including the special handles. 这将:raw层隐式地应用于每个输入或输出文件句柄,包括特殊句柄。 You can set this from the command line by using 您可以使用以下命令从命令行进行设置

perl -Mopen=IO,raw program.pl

Or you can set the PERLIO environment variable 或者您可以设置PERLIO环境变量

set PERLIO=raw

which will affect every program run henceforth from the same cmd window 这将影响以后从同一cmd窗口运行的每个程序

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在C中读取文本文件时如何阻止Windows C库将“\\ r \\ n”转换为“\\ n”? - How to stop the Windows C library from converting “\r\n” to “\n” when reading a text file in C? Python Write 在 Windows 中将“\\n”替换为“\\r\\n” - Python Write Replaces "\n" With "\r\n" in Windows 停止Windows上的sed从\\ r更改为\\ r \\ n - Stop sed on windows from changing \r to \r\n 如何在Windows中阻止fprintf将\\ r与\\ n一起打印到文件 - How do I stop fprintf from printing \r's to file along with \n in Windows 如何防止进程在 Windows 上被杀死 - How to prevent process from being killed on Windows 如何检测文件是否包含Unix换行符(\\ n)或Windows换行符(\\ r \\ n)? - How can I detect if a file has Unix line feeds (\n) or Windows line feeds (\r\n)? 在Windows命令行中无法正确识别“ \\ n” - “\n” is not being recognized properly in Windows command line 创建.mid文件:在Windows中写入“ \\ n”会导致“ \\ r \\ n” - creating .mid file: writing a '\n' causes '\r\n' in Windows 在 Mac 与 Windows 上的 Python 中处理 \\r\\n 与 \\n 换行符 - Handling \r\n vs \n newlines in python on Mac vs Windows Windows和Unix在\\ r \\ n(CRLF)和\\ n(LF)之间的可移植性错误? - Portability Bug between Windows and Unix of \r\n (CRLF) and \n (LF)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM