windows - 如何用行号替换任何行?

标签 windows batch-file replace cmd

编辑:在@aschipfl 的大力帮助下,代码达到了我希望的 %110 功能!我做了一些额外的研究,并使其易于使用,并提示额外的 %10 :P

@echo off
setlocal EnableExtensions DisableDelayedExpansion

rem // Create a prompt to set the variables
set /p _FILETYPE="What file type: "
set /p _LINENUM="Which line: "
set /p _NEWLINE="Make line say: "

rem // Start the loop, and set the files
for %%f in (*%_FILETYPE%) do (
  set "_FILE=%%f"
  echo "_FILE=%%f"

rem // To execute seperate code before the end of the loop, starting at ":subroutine".
call :subroutine "%%f"
)

:subroutine
rem // Write to a temporary file:
> "%_FILE%.new" (
    rem /* Loop through each line of the original file,
    rem    preceded by the line number and a colon `:`:*/
    for /F "delims=" %%A in ('findstr /N "^" "%_FILE%"') do (
        rem // Store the current line with prefix to a variable:
        set "LN=%%A"
        rem /* Store the line number into another variable;
        rem    everything up to the first non-numeric char. is regarded,
        rem    which is the aforementioned colon `:` in this situation: */
        set /A "NUM=LN"
        rem // Toggle delayed expansion to avoid trouble with `!`:
        setlocal EnableDelayedExpansion
        rem /* Compare current line number with predefined one and replace text
        rem    in case of equality, or return original text otherwise: */
        if !NUM! equ %_LINENUM% (
            echo(!_NEWLINE!
        ) else (
            rem // Remove line number prefix:
            echo(!LN:*:=!
        )
        endlocal
    )
)
rem // Move the edited file onto the original one:
move /Y "%_FILE%.new" "%_FILE%"

endlocal
exit /B

原问题:
任何一行中的内容都无关紧要。我只是希望能够从 .txt 中选择任何行并将其替换为我选择的任何内容。

例如:也许我有一堆 .txt,我想用“vanilla”替换所有这些文件中的第 5 行。然后选择用“Green”替换所有 .txt 的第 10 行。等等...

我见过很多人问同样的主要问题。但我一直在寻找情境答案。
“如何替换特定行?” “您搜索行中已有的内容,并将其替换为您的新文本”-我不能这样做。我需要它是动态的,因为每个“第 5 行”中的内容是不同的,或者有很多其他行具有相同的文本。

我已经尝试了我能找到的唯一一个答案,但它最终所做的只是用“!ln:*:=!”替换了字面上的所有行,而不是回声。
@echo off
setlocal disableDelayedExpansion

set "file=yourFile.txt"
set "newLine5=NewLine5Here"

>"%file%.new" (
  for /f "delims=" %%A in ('findstr /n "^" "%file%"') do for /f "delims=:" %%N in ("%%A") do (
    set "ln=%%A"
    setlocal enabableDelayedExpansion
    if "!ln:~0,6!" equ "5:FMOD" (echo(!newLine5!) else echo(!ln:*:=!
    endlocal
  )
)
move /y "%file%.new" "%file%" >nul

最佳答案

以下(注释)代码应该适合您:

@echo off
setlocal EnableExtensions DisableDelayedExpansion

rem // Define constants here:
set "_FILE=yourFile.txt"
set "_NEWLINE=NewLine5Here"
set /A "_LINENUM=5" & rem // (target line number)

rem // Write to a temporary file:
> "%_FILE%.new" (
    rem /* Loop through each line of the original file,
    rem    preceded by the line number and a colon `:`:*/
    for /F "delims=" %%A in ('findstr /N "^" "%_FILE%"') do (
        rem // Store the current line with prefix to a variable:
        set "LN=%%A"
        rem /* Store the line number into another variable;
        rem    everything up to the first non-numeric char. is regarded,
        rem    which is the aforementioned colon `:` in this situation: */
        set /A "NUM=LN"
        rem // Toggle delayed expansion to avoid trouble with `!`:
        setlocal EnableDelayedExpansion
        rem /* Compare current line number with predefined one and replace text
        rem    in case of equality, or return original text otherwise: */
        if !NUM! equ %_LINENUM% (
            echo(!_NEWLINE!
        ) else (
            rem // Remove line number prefix:
            echo(!LN:*:=!
        )
        endlocal
    )
)
rem // Move the edited file onto the original one:
move /Y "%_FILE%.new" "%_FILE%"

endlocal
exit /B

除了 EnableDelayedExpansion 中的拼写错误在您的代码中,您甚至不需要第二个 for /F循环获取行号,您不需要从前缀行文本中提取一定数量的字符。

请注意,此方法对于高于 231 - 1 = 2 147 483 647 的行号失败。

关于windows - 如何用行号替换任何行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60069126/

相关文章:

windows - windows 的 "notify-send"等效项是什么?

SQL 替换字符串中的所有字符

php - 错误代码 200 phpmyadmin 4.6.0

c# - 使用 C# 在远程计算机上设置日期、时间和时区

windows - 如何在 Windows 启动时设置 VPN

c++ - 对于网络映射驱动器形式的 C++ 代码,rsync 失败

windows - 批处理脚本循环遍历数组并将值用作exe的参数

windows-7 - Windows 批处理 : How to add Host-Entries?

regex - 如何在 perl 替换中转义 REPLACEMENT?

javascript正则表达式替换html