Perl 正则表达式:用双反斜杠替换所有反斜杠 [英] Perl regex: replace all backslashes with double-backslashes

查看:113
本文介绍了Perl 正则表达式:用双反斜杠替换所有反斜杠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一组大文件中,我需要将所有出现的\"替换为\\".为此,我想使用 Perl.现在,我有以下几点:

Within a set of large files, I need to replace all occurrences of "\" with "\\". I'd like to use Perl for this purpose. Right now, I have the following:

perl -spi.bak -e '/s/\\/\\\\/gm' inputFile

此命令是我建议,但结果是inputFile 没有变化(更新的时间戳除外).认为问题可能是\"没有被空格包围,我尝试了

This command was suggested to me, but it results in no change to inputFile (except an updated timestamp). Thinking that the problem might be that the "\"s were not surrounded by blanks, I tried

perl -spi.bak -e '/s/.\\./\\\\/gm' inputFile

同样,这对文件没有影响.最后,我想我可能缺少一个分号,所以我尝试了:

Again, this had no effect on the file. Finally, I thought I might be missing a semicolon, so I tried:

perl -spi.bak -e '/s/.\\./\\\\/gm;' inputFile

这也没有效果.我知道我的文件包含\",例如在以下行中:

This also has no effect. I know that my file contains "\"s, for example in the following line:

("C:\WINDOWS\system32\iac25_32.ax","Indeo audio)

我不确定正则表达式是否有问题,或者我调用 Perl 的方式是否有问题.我对正则表达式有基本的了解,但我绝对是 Perl 的初学者.

I'm not sure whether there is a problem with the regex, or if something is wrong with the way I'm invoking Perl. I have a basic understanding of regexes, but I'm an absolute beginner when it comes to Perl.

这里有什么明显的错误吗?我注意到的一件事是命令返回的速度非常快,尽管 inputFile 的大小约为 10MB.

Is there anything obviously wrong here? One thing I notice is that the command returns quite quickly, despite the fact that inputFile is ~10MB in size.

推荐答案

在命令行中处理反​​斜杠的难点在于知道有多少进程将操作命令行 - 以及它们的引用规则是什么.

The hard part with handling backslashes in command lines is knowing how many processes are going to manipulate the command line - and what their quoting rules are.

在 Unix 上,在任何 shell 下,您显示的第一个命令行都可以工作.

On Unix, under any shell, the first command line you show would work.

您似乎在使用 Windows,在那里,您需要处理 DOS 命令shell".

You appear to be on Windows, and there, you have the DOS command 'shell' to deal with.

我会将替换内容放入一个文件中并将其传递给 Perl:

I would put the replacement into a file and pass that to Perl:

#!/bin/perl -spi.bak
s/\\/\\\\/g;

这应该可以解决问题 - 保存为subber.pl"然后运行:

That should do the trick - save as 'subber.pl' and then run:

perl subber.pl file1 ...

这篇关于Perl 正则表达式:用双反斜杠替换所有反斜杠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆