批处理文件不插入来自另一个文件的文本

Batch file doesn't insert text from another file

我在 Windows 10 工作,我有一些文本文件来自另一个进程,我想使用批处理文件对其进行编辑。为此 post,我已经缩短了示例 File-1(下面显示了行号)。它们都采用这种格式,但更长且包含更多部分:

1 Album 1 - 1982,,,      
2 ('Song 1'),      
3 ('Song 2'),      
4 |===================|      
5 Album 2 - 1978,,,      
6 ('Song 1'),      
7 ('Song 2'),    

在每行后面加上一个由 3 个逗号组成的字符串(第 1 行和第 5 行),我想在 File-1 中的现有行(第 2 行和第 6 行)之前从 File-2 插入两行新文本。

File-2 中要插入的两行是:

NEW LINE 1
NEW LINE 2

所需的输出将是:

1 Album 1 - 1982,,,      
2 NEW LINE 1
3 NEW LINE 2
4 ('Song 1'),      
5 ('Song 2'),      
6 |===================|      
7 Album 2 - 1978,,,      
8 NEW LINE 1
9 NEW LINE 2
10 ('Song 1'),      
11 ('Song 2'),    

到目前为止,我最接近的实验是一个批处理文件(我发现并改编),它找到了 3 个逗号并将它们替换为 3 个点。

@echo off &setlocal
set "search=,,,"
set "replace=..."
(for /f "delims=" %%i in (a.txt) do (
    set "word=%%i"
    setlocal enabledelayedexpansion
    set "word=!word:%search%=%replace%!"
    echo(!word!
    endlocal
    )
)

将修改后的批处理文件输出到屏幕:

1 Album 1 - 1982...      
2 ('Song 1'),      
3 ('Song 2'),      
4 |===================|      
5 Album 2 - 1978...      
6 ('Song 1'),      
7 ('Song 2'),    

我不需要将逗号更改为点(这只是一个测试,看看我是否可以让批处理文件停止并在每个唯一字符串实例上做一些事情)。

我的困境是我已经搜索了几个小时,但找不到如何在 File-1 的两个适当位置插入 File-2 文本。我找到了很多关于如何在一个点或第一个文件的末尾插入另一个文件的文本(使用“type”命令)的解释,但不是在搜索中找到的多个特定点。另外,未来File-2的文本可能会超过两行,所以我想用一个外部文件来保存它。

我现在真正想做的是:

  1. 查找已知字符串(3 个逗号)。
  2. 从文本文件中读取样板文本并将其插入到包含找到的字符串的行下方,放入批处理文件内存中的文件中。

为了测试,我将其回显到屏幕上,但是一旦我能够获得正确的输出,我就可以编辑批处理文件以将输出重定向到一个文件。我知道使用 python 或 C 有更简单的方法,但看起来应该很容易通过一个小的修正来做到这一点。

更新

我根据@aschipfl 更正了我的批处理文件,并制作了新的输入文件以便于调试。

a.txt:

A LINE 1,,,
A LINE 2

b.txt:

B LINE 1
B LINE 2

我有条不紊地尝试了三个版本的 FIND 语句,从 @aschipfl 原始版本开始,但没有与 b.txt.

交互
if not "!word!"=="!word:%search%=!" find /V ",,," < "!infile2!" 
if not "!word!"=="!word:%search%=!" < "!infile2!"  find /V ",,,"
if not "!word!"=="!word:%search%=!" find /V ",,," type "!infile2!"

第四种组合 与 b.txt 交互,如下所示。

if not "!word!"=="!word:%search%=!" type "!infile2!"  find /V ",,,"
C:\Users\Pete\Documents\test>test.bat
A LINE 1,,,

b.txt


B LINE 1
B LINE 2The system cannot find the file specified.
Error occurred while processing: find.
The system cannot find the file specified.
Error occurred while processing: /V.
The system cannot find the file specified.
Error occurred while processing: ,,,.
A LINE 2

C:\Users\Pete\Documents\test>

现在,批处理文件读取了两个输入文件的两行,但出现了如图所示的错误(完整的空白行,直接从屏幕上复制)。

您不应该尝试用 ,,, + 换行符 + 来自另一个文件的多行文本替换 ,,,,您应该在 echo(!word! 之后检查当前文件是否行包含 if not "!word!"=="!word:,,,=!",,,,如果是,只需按 type "File-2.txt"find /V "" < "File-2.txt" 键入另一个文本文件,以防其内容可能不会以最终结尾换行符:

@echo off
setlocal EnableExtensions DisableDelayedExpansion

rem // Define constants here:
set "infile1=%~dp0File-1.txt"
set "infile2=%~dp0File-2.txt"
set "outfile=con"
set "search=,,,"

> "%outfile%" (
    rem // The strange unquoted option string syntax disables the `eol` character:
    for /F usebackq^ delims^=^ eol^= %%i in ("%infile1%") do (
        set "word=%%i"
        setlocal EnableDelayedExpansion
        echo(!word!
        rem // Try to remove search string; if result differs, search string occurred:
        if not "!word!"=="!word:%search%=!" find /V "" < "!infile2!"
        endlocal
    )
)
endlocal
exit /B

cmd batch-file 中执行此操作的一种方法是使用 PowerShell。如果您使用的是受支持的 Windows 平台,则可以使用 PowerShell。这使用 regex 插入另一个文件的内容。

powershell -NoLogo -NoProfile -Command ^
    (Get-Content -Path .\afile1.txt) -replace ',,,', ^
        \",,,$([Environment]::NewLine)$(Get-Content -Raw -Path .\afile2.txt)\"

示例:

C:>type afile1.txt
1 Album 1 - 1982,,,
2 ('Song 1'),
3 ('Song 2'),
4 |===================|
5 Album 2 - 1978,,,
6 ('Song 1'),
7 ('Song 2'),

C:>type afile2.txt
NEW LINE 1
NEW LINE 2

C:>powershell -NoLogo -NoProfile -Command ^
More?     (Get-Content -Path .\afile1.txt) -replace ',,,', ^
More?         \",,,$([Environment]::NewLine)$(Get-Content -Raw -Path .\afile2.txt)\"
1 Album 1 - 1982,,,
NEW LINE 1
NEW LINE 2
2 ('Song 1'),
3 ('Song 2'),
4 |===================|
5 Album 2 - 1978,,,
NEW LINE 1
NEW LINE 2
6 ('Song 1'),
7 ('Song 2'),