Windows 批处理脚本,用于在行中存在另一个字符串时替换某些字符串

问题描述 投票:0回答:2

我是 Windows 中批量编辑的新手,我对无法在 bat 文件中使用 PowerShell 命令感到非常困惑。

长话短说,我有这个输入文件:

The quick brown fox jumped over the lazy white dog
The quick red fox jumped over the lazy dog
The quick green wolf jumped over the lazy brown dog
The quick brown fox jumped over the lazy dog
The quick red lion jumped over the lazy dog
The quick green pig jumped over the lazy brown dog
The quick brown fox jumped over the lazy dog

如果句子包含术语

brown
,我想用
white
替换
fox

作为额外要求,我想仅在第一次出现时替换

brown

我尝试使用

.contains
find
,但当我尝试使用
if
条件的结果时,一切仍然变得混乱。

我能做到的最好的就是这个;但它仍然是垃圾。

@echo on &setlocal
set "search=brown"
set "replace=white"
set "textfile=C:\JavaLogs\ReplaceDemo.txt"
set "newfile=C:\JavaLogs\Output.txt"
set lines=0
(
for /f "delims=" %%i in (%textfile%) do (
    set lines=echo %%i | findstr /C:"fox" |  measure -w -c -l
    echo %lines%
    if "%lines%">"0" echo "There is a fox"
    if "%lines%"=="0" echo "There is no fox"
    set %%i=%%i:%search%=%replace% echo(%%i>%newfile%)  

    else echo(!%%i!)>"%newfile%"
    ) 
)

最终结果将是这样的输出文件:

The quick white fox jumped over the lazy white dog
The quick red fox jumped over the lazy dog
The quick green wolf jumped over the lazy brown dog
The quick white fox jumped over the lazy dog
The quick red lion jumped over the lazy dog
The quick green pig jumped over the lazy brown dog
The quick white fox jumped over the lazy dog
string powershell batch-file batch-processing
2个回答
0
投票

我建议使用 PowerShell,特别是如果您是新手并且在 Windows 下学习脚本编写。

您想要在 PowerShell 下执行的操作示例是创建一个包含以下内容的 MyReplace.ps1 文件:

param (
    [Parameter(Mandatory=$true)]
    [string]$Text
)
if ($Text -match 'fox') {
    $Text -replace '(?=brown)brown(.*)', 'white$1'
}
else { 
    $Text
}

然后您可以从 PowerShell 控制台窗口运行:

Get-Content C:\JavaLogs\ReplaceDemo.txt | % { .\MyReplace.ps1 $_ } 

这将运行您想要的逻辑并输出您的文本。

最后,如果您想将输出发送到另一个文件,您可以将上面的内容更改为:

Get-Content C:\JavaLogs\ReplaceDemo.txt | % { .\MyReplace.ps1 $_ } |  Set-Content C:\JavaLogs\Output.txt

0
投票
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
rem The following settings for the directories and filenames are names
rem that I use for testing and deliberately includes spaces to make sure
rem that the process works using such names. These will need to be changed to suit your situation.

SET "sourcedir=u:\your files"
SET "destdir=u:\your results"
SET "filename1=%sourcedir%\q77026292.txt"
SET "outfile=%destdir%\outfile.txt"

SET "prerequisite=fox"
SET "replace=brown"
SET "replacement=white"

(
FOR /f "usebackqdelims=" %%e IN ("%filename1%") DO (
 rem line is in %%e
 SET "foundpre="
 FOR %%o IN (%%e) DO IF "%%o"=="%prerequisite%" SET "foundpre=y"
 IF DEFINED foundpre (
  SET "outline="
  SET "replace1=y"
  FOR %%o IN (%%e) DO IF DEFINED replace1 (
   IF "%%o"=="%replace%" (
    SET "replace1="
    SET "outline=!outline! %replacement%"
   ) ELSE SET "outline=!outline! %%o"
  ) ELSE SET "outline=!outline! %%o"
  ECHO !outline:~1!
 ) ELSE ECHO %%e
)
)>"%outfile%"

GOTO :EOF

请注意,如果文件名不包含空格等分隔符,则

usebackq
%filename1%
周围的引号都可以省略。

请注意使用

delayedexpansion
Stephan 的 DELAYEDEXPANSION 链接,因为
outline
的值在
for %%e
循环中发生变化,并且布尔变量在其运行时状态上进行解释。

© www.soinside.com 2019 - 2024. All rights reserved.