拨开荷叶行,寻梦已然成。仙女莲花里,翩翩白鹭情。
IMG-LOGO
主页 文章列表 如何使用批处理将txt档案中的一行替换为新行

如何使用批处理将txt档案中的一行替换为新行

白鹭 - 2022-01-25 2590 0 0

我需要一点帮助

我有一个无法制作的脚本,我确实尝试过以不同的方式重写它

这是我的脚本

@echo off
set "replace="code1": ,"
set "replaced="code1": 99999999,"

set "source=File.txt"
set "target=result.txt"

setlocal enableDelayedExpansion
(
   for /F "tokens=1* delims=:" %%a in ('findstr /N "^" %source%') do (
      set "line=%%b"
      if defined line set "line=!line:%replace%=%replaced%!"
      echo(!line!
   )
) > %target%
endlocal

我有一个 12KB 大小的文本档案,里面有我想用其他资料替换的代码

我的目标是一个可以用新行替换整行的脚本 我有很多代码,所以我需要帮助才能读取输入的每个代码,我拥有的代码数量是未知的 我
在这里构建的代码是我的例子

file.txt
---content inside the file
"code1": 123456789,
"code2": 123123123,
"code3": 456456456,
New Codes to replace old codes
"code1": 9999999,
"code2": 9999999,
"code3": 9999999,

是否可以制作这样的脚本请注意这个较低的脚本只是一个例子

@echo off
set "source=File.txt"
set "target=result.txt"

setlocal enableDelayedExpansion
(
   for /F "tokens=1* delims=:" %%a in ('findstr /N "^" %source%') do (
      set "line=%%b"
      if defined line set "line=!line:%replace%=%replaced%!"
      echo(!line!

set "find=" - if the script can find "code1": and replace it with New "code1": 
"code1":
"code2":
"code3":

set "replace="
"code1": 99999999,
"code2": 99999999,
"code3": 99999999,

   )
) > %target%
endlocal

我已经制作了 5 个其他脚本,但我无法用它来替换整行

我还想洗掉 %target% 并让它编辑当前的 file.txt
我有一个想法这样做

 ) > %target% to  ) >> %source%

不确定是否需要其他任何东西来洗掉目标

Any help will be great

uj5u.com热心网友回复:

根据我的理解,以下脚本应该执行您想要的操作:

@echo off
setlocal EnableExtensions DisableDelayedExpansion

rem // Define constants here:
set "_FILE=%~dp0file.txt" & rem // (path to file to be edited; `%~dp0` is script parent)
set "_CODE=%~dp0code.txt" & rem // (path to file containing new codes to become applied)
set "_TMPF=%~dp0file.tmp" & rem // (path to temporary file)

rem // Write to temporary file:
> "%_TMPF%" (
    rem // Loop through lines of file, each one preceded with line number plus `:`:
    for /F "delims=" %%L in ('findstr /N "^" "%_FILE%"') do (
        rem // Store current line (including line number prefix):
        set "LINE=%%L"
        rem // Try to extract text portion between prefix and next `:`:
        for /F "tokens=2 delims=:" %%K in ("%%L") do (
            rem // Check whether text portion is `"` plus anything plus `"`:
            cmd /V /D /C echo(!LINE:*:^=!| findstr "^\"[^^^":][^^^":]*\"" > nul && (
                rem // Text portion complies with pattern, hence store such:
                set "CODE=%%K"
                rem // Toggle delayed expansion to avoid troubles with `!` and `^`:
                setlocal EnableDelayedExpansion
                rem // Escape `findstr` meta-characters `\` and `"` by `\\` and `\"`:
                for %%J in ("\" ""^") do set "CODE=!CODE:%%~J=\%%~J!"
                rem // Return line beginning with same text portion from other file:
                findstr /B /C:"!CODE!:" "!_CODE!"
                endlocal
            ) || (
                rem // Text portion does not comply, hence return original line:
                setlocal EnableDelayedExpansion
                echo(!LINE:*:=!
                endlocal
            )
        )
    )
)
rem // Replace original file:
move /Y "%_TMPF%" "%_FILE%"

endlocal
exit /B

您需要在脚本顶部指定一些档案路径:

  • 变量_FILE必须指向包含原始代码的档案:

  • 变量_CODE必须指向包含要使用的新代码的档案:

  • 变量_TMPF只是指向一个临时档案;

uj5u.com热心网友回复:

可悲的是,我们不知道该档案的确切外观,但这应该可以作业:

@echo off
set "source=File.txt"
set "target=result.txt"
setlocal enableDelayedExpansion
(
   for /F "tokens=1,2,* delims=:" %%a in ('findstr /N "^" "%source%"') do (
      echo %%b|findstr /b "\"code1\" \"code3\"" >nul && (
         echo %%b: 999999999,
      ) || (
        if "%%c" == "" (echo(%%b) else echo %%b:%%c
      )
   )
)> "%target%"
endlocal
type "%target%"
REM move /y "%source%" "%target%"

只需将您的 15 个代码放入findstr /b搜索掩码中。

注意:这应该适用于您的 15 个代码,但搜索findstr模式是有限的,因此当代码太多(或太长)时,您可能会遇到问题。

标签:

0 评论

发表评论

您的电子邮件地址不会被公开。 必填的字段已做标记 *