iT邦幫忙

1

Windows7 DOS Batch批次檔問題-文字檔 merge

請問各位大神們,小弟目前電腦有上百個文字檔內容格式如下,
檔名:0000_MMDDYYYY.txt;1111_MMDDYYYY.txt;2222_MMDDYYYY.txt.............
內容大致上如下,
number1 = XXXXXX
number2 = ZZZZZZ
PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP
OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO
...............................
想把這些文字檔案的內容合成為一個文字檔,但合成後的內容不要把number1 = XXXXXX和number2 = ZZZZZZ寫入,且格式想變為
0000_MMDDYYYY,ZZZZZZ,PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP
0000_MMDDYYYY,ZZZZZZ,OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO
1111_MMDDYYYY,ZZZZZZ,PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP
1111_MMDDYYYY,ZZZZZZ,OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO
請問使用DOS批次檔如何達成呢?
有試著寫了一個批次檔,但會變成
0000_MMDDYYYY,number1 = XXXXXX
0000_MMDDYYYY,number2 = ZZZZZZ
0000_MMDDYYYY,PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP
0000_MMDDYYYY,OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO
1111_MMDDYYYY,number1 = XXXXXX
1111_MMDDYYYY,number2 = ZZZZZZ
1111_MMDDYYYY,PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP
1111_MMDDYYYY,OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO

看更多先前的討論...收起先前的討論...
fuzzylee1688 iT邦研究生 3 級 ‧ 2019-01-10 08:41:02 檢舉
想法子剔除 MMDDYYYY,number 字串的record即可.
slime iT邦大師 1 級 ‧ 2019-01-10 23:59:43 檢舉
個人覺得用試算表軟體的巨集可能快一點....
一定要用批次檔的話, 可能會想再找 awk 軟體來輔助.
蟹老闆 iT邦大師 1 級 ‧ 2019-01-11 13:07:55 檢舉
你的問題
>>但合成後的內容不要把number1 = XXXXXX和number2 = ZZZZZZ寫入
你給的例子
0000_MMDDYYYY,ZZZZZZ,PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP
是要ZZZZZZ還是不要呢?
檔案1檔名為 computer1_01162019.txt
內文如下:
machine: computer1
number: 變數1

000001 aaa 11111 11111 11111
000002 bbb 22222 22222 22222
.
000026 zzz 26262 26262 26262
-----------------
檔案2檔名為 computer2_01162019.txt
內文如下:
machine: computer2
number: 變數2

000001 aaa 11111 11111 11111
000002 bbb 22222 22222 22222
.
000026 zzz 26262 26262 26262
=================
想透過批次檔將檔案1和檔案2的內容寫到同一個文字檔,
computer1_01162019, 變數1, 000001 aaa 11111 11111 11111
computer1_01162019, 變數1, 000002 bbb 22222 22222 22222
.
computer1_01162019, 變數1, 000026 zzz 26262 26262 26262
computer2_01162019, 變數2, 000001 aaa 11111 11111 11111
computer2_01162019, 變數2, 000002 bbb 22222 22222 22222
.
computer2_01162019, 變數2, 000026 zzz 26262 26262 26262
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

2 個回答

0
蟹老闆
iT邦大師 1 級 ‧ 2019-01-17 01:41:07
最佳解答

請參考
注意執行時會刪除前次儲存的記錄檔(integrate.Log)

@ECHO OFF
Set Integrate=integrate.Log
Del /Q %integrate% 1 2>Nul
For /f "Delims=: Tokens=1-4" %%i In ('Findstr /N /R /C:"^number:\>" *.txt') Do Call :Write %%j %%i %%l
Exit /b
:Write
For /f "Skip=%1 Delims=*" %%o In ('Findstr . "%2"') Do Echo %%~ni,%3,%%o >>%Integrate%

有試著用script把一堆文字檔抓回自已的電腦中再執行哥提供的script後已經可以combine在一起了,但看了for /?後還是霧煞煞,可以請哥幫忙指導解析一下你寫的這段嗎,想學習一下如何應用,感謝你.

0
haoming
iT邦好手 1 級 ‧ 2019-01-10 15:04:07

麻煩描述更清楚一點

檔案1檔名為 computer1_01162019.txt
內文如下:
machine: computer1
number: 變數1

000001 aaa 11111 11111 11111
000002 bbb 22222 22222 22222
.
000026 zzz 26262 26262 26262

檔案2檔名為 computer2_01162019.txt
內文如下:
machine: computer2
number: 變數2

000001 aaa 11111 11111 11111
000002 bbb 22222 22222 22222
.
000026 zzz 26262 26262 26262

想透過批次檔將檔案1和檔案2的內容寫到同一個文字檔,
computer1_01162019, 變數1, 000001 aaa 11111 11111 11111
computer1_01162019, 變數1, 000002 bbb 22222 22222 22222
.
computer1_01162019, 變數1, 000026 zzz 26262 26262 26262
computer2_01162019, 變數2, 000001 aaa 11111 11111 11111
computer2_01162019, 變數2, 000002 bbb 22222 22222 22222
.
computer2_01162019, 變數2, 000026 zzz 26262 26262 26262

我要發表回答

立即登入回答