[英]Find until that line and replace with another text file windows batch script
[英]batch script, find string in text file by line then replace whole line with another string
我想編寫一個批處理文件,以便按行在文本文件中找到給定的字符串,然后用另一給定的字符串替換整行。
示例:如果text.txt中的每一行都包含string_replace,則具有文件text.txt string_replace = abc string_replace_with = xyz,因此該行將替換為string_replace_with
誰能幫我 ? 非常感謝
@echo off
SET InFile=test.txt
FOR /F "tokens=*" %%A IN ('FINDSTR "xyz" "%InFile%"') DO CALL :FindString "%%A"
pause
goto :eof
:FindString
SET String=%~1
SET String=%String:xyz=abc%
echo.%String%>getout.txt
如果可以解決問題,請嘗試這些。 從您的問題中我了解到,您想找到所需的單詞並將其替換為所需的單詞。
為自己准備一個Windows sed
端口。
如果您以這種方式啟動此SAR.BAT文件,它將用xyz替換abc
SAR“ fileinput.txt”“ fileoutput.txt”“ abc”“ xyz”
::Search and replace
@echo off
if "%~3"=="" (
echo.Search and replace
echo Syntax:
echo %0 "filein.txt" "fileout.ext" "regex" "replace_text" [first]
echo.
echo. if [first] is present only the first occurrence is changed
goto :EOF
)
if "%~5"=="" (set global=true) else (set global=false)
set s=regex.replace(wscript.stdin.readall,"%~4")
>_.vbs echo set regex=new regexp
>>_.vbs echo regex.global=%global%
>>_.vbs echo regEx.IgnoreCase=True
>>_.vbs echo regex.pattern="%~3"
>>_.vbs echo wscript.stdOut.write %s%
cscript /nologo _.vbs <"%~1" >"%~2"
del _.vbs
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.