2017-02-02 94 views
-1

我有一個看起來像這樣插入特定的文本

0181830207709014000015576526S00000140000014000001242017
0052500056661095000015576527S00000950000095000001242017
0157020294103030000015576528S00000300000030000001242017
2397870060994031500015576529S00000315000031500001242017
2466260219154015000015576530S00000150000015000001242017

我需要插入所謂的「特定的文本文件給我插入一個ne w行「並將輸出重定向到具有不同文件名的新文件。我怎樣才能解決這個使用批處理編程,

+1

您首先搜索類似的問題和答案對或提供您現有的代碼。解釋你如何運行該代碼,它做了什麼,你期望發生什麼以及你需要什麼幫助。 _(請理解這不是免費的寫作服務)_ – Compo

回答

0

使用Findstr /N到編號的文件

> findstr /N ".*" test.txt 
1:0181830207709014000015576526S00000140000014000001242017 
2:0052500056661095000015576527S00000950000095000001242017 
3:0157020294103030000015576528S00000300000030000001242017 
4:2397870060994031500015576529S00000315000031500001242017 
5:2466260219154015000015576530S00000150000015000001242017 

而且一個For /f環的線路到瓦爾%%A的號碼解析輸出,分裂在結腸和行內容爲%%B。如果號碼是2請插入額外的行。

@Echo off 
Set "File=Test.txt" 
Set "NewFile=TestNew.txt" 
(
    For /f "Tokens=1*Delims=:" %%A in (
    'Findstr /N ".*" "%File%"' 
) Do If %%A Equ 2 (
    Echo:insert me a new line before 
    Echo:%%B 
    Echo:insert me a new line below 
) Else (
    Echo:%%B 
) 
) >"%NewFile%" 

EDIT改變了邏輯後續的讀者。示例輸出:

> type TestNew.txt 
0181830207709014000015576526S00000140000014000001242017 
insert me a new line before 
0052500056661095000015576527S00000950000095000001242017 
insert me a new line below 
0157020294103030000015576528S00000300000030000001242017 
2397870060994031500015576529S00000315000031500001242017 
2466260219154015000015576530S00000150000015000001242017 
+0

好的。那工作。我甚至不知道findstr/N表達式。謝謝 – neewbee

+0

@neewbee你可能會考慮[接受答案](http://stackoverflow.com/help/accepted-answer) – LotPings

相關問題