简体   繁体   English

Perl oneliner损坏了Windows中的文件(与回车有关的问题)

[英]Perl oneliner corrupts file in Windows (carriage-return related issue)

I have a PostScript file, in which I want to change one line from: 我有一个PostScript文件,我要在其中更改一行:

%%Title: my abc %%标题:我的abc

to

%%Title: your def %%标题:您的定义

The following Perl oneliner did the trick in Ubuntu: 以下Perl oneliner在Ubuntu中达到了目的:

perl -p -ifoo.bak -e "s/%%Title:.+\n/%%Title: your def\n/" file.ps

but it corrupts the file in Windows (looks like it adds \\r\\n to every "line" or something like that). 但会损坏Windows中的文件(看起来它在每个“行”中都添加了\\ r \\ n或类似内容)。

I tried another approach, but still no luck in Windows: 我尝试了另一种方法,但在Windows中仍然没有运气:

perl -Mopen=OUT,:bytes -p -ifoo.bak -e "s/%%Title:.+\n/%%Title: your def\n/" file.ps

What is going on and how do I solve that problem? 发生了什么事,我该如何解决这个问题? Thank you. 谢谢。

Remove the line break from pattern and replacement: 从样式和替换中删除换行符:

perl -p -ifoo.bak -e "s/%%Title:.+$/%%Title: your def/" file.ps

or capture and use it in replacement: 或捕获并用于替换:

perl -p -ifoo.bak -e "s/%%Title:.+(\R)/%%Title: your def$1/" file.ps

\\R stands for any kind of line break. \\R代表任何换行符。

If you want to transform whatever the actual line break is to unix linebreak (ie. \\n ): 如果要将实际的换行符转换为unix换行符(即\\n ):

perl -p -ifoo.bak -e "s/%%Title:.+\R/%%Title: your def\n/" file.ps

In windows end of line are \\r\\n and not \\n like in unix so 在Windows行尾是\\ r \\ n而不是\\ n,就像在UNIX中一样

perl -p -ifoo.bak -e "s/%%Title:.+\r\n/%%Title: your def\r\n/" file.ps

may works 可能有效

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM