[英]how to replace a string on the second line in a text file using a batch file?
[英]Change a string within a line from a text file using batch
我有文本文件(保存為* .pm,但我認為它沒有任何區別,因為它可以作為文本文件打開)。 該文件的內容類似於:
Header
$VAR1 = {
'Keywords' => {
'X' => '80',
'Target_Path' => 'Example_PAth',
'Y' => 'File_Path',
'X' => '80',
'Y' => 'File_Path',
'X' => '80',
'Y' => 'File_Path',
sdefkjnhksdf koijsef ökiljsdf
sdefkjnhksdf koijsef ökiljsdf
sdefkjnhksdf koijsef ökiljsdf
我想改變EXAMPLE_PATH使用* .bat文件來Example_PAth_New。
為此,我有以下代碼:
@echo off
setlocal disableDelayedExpansion
:Variables
set InputFile=OldFileName.pm
set OutputFile=NewFileName.pm
set "_strFind=Example_PAth"
set "_strInsert=Example_PAth_New"
:Replace
>"%OutputFile%" (
for /f "usebackq delims=" %%A in ("%InputFile%") do (
if "%%A" equ "%_strFind%" (echo %_strInsert%) else (echo %%A)
)
)
更新:我現在修改了搜索和替換的字符串,以包括@Squashman提到的整行。 如果搜索的字符串不包含>字符,則可以完美地工作。 如果我保留> ,則可以正確檢測到該行,但將其替換為空白行。 任何解決方法? PS:我不能省略>字符。
我根據12個小時前給出的建議修改了原始代碼,並對其進行了測試以證明其有效(在10個小時前OP在Update中表示尚無解決方案之后)。
@echo off
setlocal EnableDelayedExpansion
:Variables
set InputFile=OldFileName.pm
set OutputFile=NewFileName.pm
set "_strFind=Example_PAth"
set "_strInsert=Example_PAth_New"
:Replace
>"%OutputFile%" (
for /f "usebackq delims=" %%A in ("%InputFile%") do (
set "line=%%A"
echo !line:%_strFind%=%_strInsert%!
)
)
輸出文件:
Header
$VAR1 = {
'Keywords' => {
'X' => '80',
'Target_Path' => 'Example_PAth_New',
'Y' => 'File_Path',
'X' => '80',
'Y' => 'File_Path',
'X' => '80',
'Y' => 'File_Path',
sdefkjnhksdf koijsef ökiljsdf
sdefkjnhksdf koijsef ökiljsdf
sdefkjnhksdf koijsef ökiljsdf
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.