我在 Windows 10 中作業,我有另一個行程產生的文本檔案,我想使用批處理檔案進行編輯。我已經為這篇文章縮短了示例 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 文本可能會超過兩行,這就是為什么我想使用外部檔案來保存它的原因。
我現在真正想做的是:
- 查找已知字串(3 個逗號)。
- 從文本檔案中讀取樣板文本,并將其插入到帶有找到的字串的行下方,插入位于批處理檔案記憶體中的檔案中。
For testing, I am echoing this to the screen, but once I am able to get the right output, I can edit the batch file to redirect output to a file. I know there are easier ways of doing this using python or C, but it seems it should be easy to do this way with a small correction.
UPDATE
I corrected my batch file per @aschipfl, and made new input files for easier debugging.
a.txt:
A LINE 1,,,
A LINE 2
b.txt:
B LINE 1
B LINE 2
I methodically tried three versions of the FIND statement starting with the @aschipfl original but there is no interaction with 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!"
The fourth combination does interact with b.txt as shown below it.
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>
Now, the batch file reads both lines of both input files but with errors as shown (complete with blank lines, copied directly from screen).
uj5u.com熱心網友回復:
您不應該嘗試,,,用,,, 換行符 來自另一個檔案的多行文本替換,而應該在 之后echo(!word!檢查當前行是否包含,,,by if not "!word!"=="!word:,,,=!",如果是,只需鍵入另一個文本檔案 bytype "File-2.txt"或 by find /V "" < "File-2.txt"in如果它的內容可能不會以最后的換行符終止:
@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
uj5u.com熱心網友回復:
在 a 中執行此操作的一種cmd batch-file方法是使用 PowerShell。如果您在受支持的 Windows 平臺上,則可以使用 PowerShell。這使用 aregex來插入另一個檔案的內容。
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'),
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/409887.html
標籤:
上一篇:單贏表單視窗上的流程
