windows 正则表达式搜索批量替换

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7023460/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 17:48:23  来源:igfitidea点击:

regex search replace in batch

regexwindowsbatch-file

提问by standatw

I want to do search/replace with a regex pattern in windows batch on a set of files. It will be something like:

我想在 Windows 批处理中对一组文件使用正则表达式模式进行搜索/替换。它会是这样的:

if the regex matches a line matches then replace it with a new line.

also I need to add a variable in the regex to just replace the value. Does anyone know how to deal with the batch script? I'm not that familiar with it. Some examples maybe helpful.

我还需要在正则表达式中添加一个变量来替换该值。有谁知道如何处理批处理脚本?我对它不是很熟悉。一些例子可能会有所帮助。

回答by Hnatt

You can benefit from findstrcommand. It supports regular expressions with limited syntax. Say, you have files

您可以从findstr命令中受益。它支持语法有限的正则表达式。说,你有文件

ignore_me.txt
rename_me01.txt
rename_me02.txt
rename_me03.txt
rename_me04.txt

A command

一个命令

dir /b | findstr "rename_me[0-9][0-9]"

will output

会输出

rename_me01.txt
rename_me02.txt
rename_me03.txt
rename_me04.txt

OK, not very good example, because you can do this with good old ?wildcard. My point is that pattern for findstris a regular expression.

好的,不是很好的例子,因为你可以用很好的旧?通配符来做到这一点。我的观点是模式 forfindstr是一个正则表达式。

Recently I had a similar problem, but I bored trying to find out how to replaceregex patterns. I'm not sure if it even possible with only native Windows commands. So, for more simple, but still native solution I referred to Windows Scripting Host. My task was to find all files that have in their names a date in format dd.mm.yyyyand replace that date with current.

最近我遇到了类似的问题,但我厌倦了试图找出如何替换正则表达式模式。我不确定仅使用本机 Windows 命令是否可行。因此,对于更简单但仍然是本机的解决方案,我提到了Windows Scripting Host。我的任务是查找名称中包含dd.mm.yyyy格式日期的所有文件,并将该日期替换为当前日期。

The script for that is:

脚本是:

<job>
    <script language="JavaScript">
        var d = new Date();
        // get current date for replacement
        var currDate = (d.getDate() + "." + (d.getMonth() + 1) + "." + d.getFullYear())
            // add leading zeros to date and month
            .replace(/^([0-9])\./g, '0.').replace(/\.([0-9])\./g, '.0.');
        var fso = new ActiveXObject("Scripting.FileSystemObject");  
        var files = new Enumerator(fso.getFolder(".").files);
        var count = 0;
        for (; !files.atEnd(); files.moveNext())
        {
            var file = ""+files.item(); // make it string
            if (file.match(/[0-9]{2}\.[0-9]{2}\.[0-9]{4}/)) {
                var newName = file.replace(/[0-9]{2}\.[0-9]{2}\.[0-9]{4}/, currDate);
                fso.moveFile(file, newName);
                count++;
            }
        }
        WScript.echo("Renamed "+count+" files");
    </script>
</job>

Saved it under name u.wsfand put into the folder to those files. The extension is associated with wscriptso when double clicking on the file it runs in GUI mode, but can also run in command line mode:

以名称保存u.wsf并放入文件夹中的那些文件。该扩展名与wscriptso 当双击它在 GUI 模式下运行的文件相关联,但也可以在命令行模式下运行:

cscript u.wsf

回答by tripleee

Standard tools on Unix for this are sedand Perl. There are DOS/Windows ports of both.

Unix 上的标准工具是sed和 Perl。两者都有 DOS/Windows 端口。

A:\> sed -i "s/foo/bar/g" file1 file2 file3

A:\> perl -pi -e "s/foo/bar/g" file1 file2 file3

will replace "foo" with "bar" in the named files. Sed is older and the -i option is not standardized, and Perl is a lot more versatile, being a full-blown programming language, and its regex feature set is much better, but for a simple one-off job, either is fine.

将命名文件中的“foo”替换为“bar”。Sed 较旧且 -i 选项未标准化,而 Perl 用途更广,是一种成熟的编程语言,其正则表达式功能集要好得多,但对于简单的一次性工作,两者都可以。