从 Windows 批处理文件替换文本文件中的字符

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

Replacing characters in a text file from Windows batch file

windowsbatch-filetext

提问by Wally Walters

I'm trying to write a batch file that will look at every character in a small text file (a CUE sheet, actually) and do three things -- remove all question marks, replace any slash marks with a hyphen and replace colons with two hyphens -- as per the second line in the example below.

我正在尝试编写一个批处理文件,该文件将查看小文本文件(实际上是 CUE 表)中的每个字符,并执行三件事——删除所有问号,用连字符替换任何斜杠,用两个替换冒号连字符 - 根据下面示例中的第二行。

  TRACK 01 AUDIO
    TITLE "Colon:  Slash / Question Mark?"
(would be changed to)
    TITLE "Colon -- Slash - Question Mark"

I know how to use findstr to copy the lines containing those characters to a new text file (but only those lines), and not how to do the search-and-replace. And findstr also has the unwanted consequence of removing leading spaces, which I wish to retain. Not sure what's the right approach here. (I should add that for various reasons I prefer not to use a third-party utility.)

我知道如何使用 findstr 将包含这些字符的行复制到一个新的文本文件(但仅限于那些行),而不是如何进行搜索和替换。而且 findstr 还具有删除前导空格的不良后果,我希望保留这些空格。不确定这里的正确方法是什么。(我应该补充一点,出于各种原因,我不喜欢使用第三方实用程序。)

回答by Baljeetsingh

You can use Powershell to solve this very easily.

您可以使用 Powershell 非常轻松地解决此问题。

Open PowerShell and type this

打开PowerShell并输入

(Get-Content C:\Users\Admin\StackOverflow\some.txt).Replace(":"," - -") | Set-Content C:\Users\Admin\StackOverflow\SomeModified.txt

Basically,

基本上,

Get-Contentwill open the file.

Get-Content将打开文件。

.Replaceto replace the text(old text, new text)

.Replace替换文本(旧文本、新文本)

| Set-Content< path > to output on diffrent file.

| 设置内容<路径> 以在不同的文件上输出。

ScreenshotsSource FilePowershell CommandDestination File Post Change

截图源文件PowerShell 命令目标文件更改后

回答by Magoo

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION 
SET "sourcedir=U:\sourcedir"
SET "destdir=U:\destdir"
SET "filename1=%sourcedir%\q42482508.txt"
SET "outfile=%destdir%\outfile.txt"
(
FOR /f "usebackqdelims=" %%a IN ("%filename1%") DO (
 SET "line=%%a"
 SET "line=!line:?=!"
 SET "line=!line:/=-!"
 SET "line=!line::=--!"
 ECHO !line!
)
)>"%outfile%"

GOTO :EOF

You would need to change the settings of sourcedirand destdirto suit your circumstances.

您需要更改的设置sourcedir,并destdir以适合你的情况。

I used a file named q42482508.txtcontaining your data for my testing.

我使用了一个名为q42482508.txt包含您的数据的文件进行测试。

Produces the file defined as %outfile%

生成定义为 %outfile% 的文件

Invoke delayed expansion to allow strings to be manipulated in a code block.

调用延迟扩展以允许在代码块中操作字符串。

Read each line of the file to %%athence line; replace each ?with nothing, /with one -and :with two, and echothe result.

读取文件的每一行到%%a那里line;替换每个?没有/有一个-:两个,和echo结果。

Parenthesising the forallows redirection to the specified file.

括号for允许重定向到指定的文件。

The pattern is set "var1=!var2:stringtoreplace=replacementstring!" as documented, !in place of %to use delayedexpansion.

模式设置为 "var1=!var2: stringtoreplace= replacementstring!" 如文档所述,!代替%使用延迟扩展。

Note that your text specifies the replacemnt string for :as --whereas your example shows it being replaced by "Space--"

请注意,您的文本指定了替换字符串:as--而您的示例显示它被替换为“ Space--”