通过分隔符将文本文件拆分为2个文件

时间:2013-05-17 06:58:02

标签: windows batch-file cmd

我正在寻找一个简单的批处理脚本,将我的文本文件拆分为两个文件。文本文件用

分隔
============================================================================

文本文件如下所示:

test
test1
test2
test5
test
test1
test2
test5
============================================================================
test
test1
test2
test5
test
test1   

2 个答案:

答案 0 :(得分:3)

试试这个:

@echo off&setlocal
set "file=file.txt"
for /f "delims=[]" %%i in ('^<"%file%" find /n "="') do set "split=%%i"
(for /f "tokens=1*delims=[]" %%i in ('^<"%file%" find /n /v ""') do if %%i lss %split% echo(%%j)>"%file%.new1"
<"%file%">"%file%.new2" more +%split%
type "%file%.new?"

..输出是:

file.txt.new1


test
test1
test2
test5
test
test1
test2
test5

file.txt.new2


test
test1
test2
test5
test
test1

答案 1 :(得分:2)

@echo off
set /p file=FILE TO PROCESS : 
del /q /f out.file*
setlocal enableDelayedExpansion
set out_file_counter=1
for /f "usebackq delims=" %%L in ("%file%") do (
 set line=%%L
 if "!line:~0,5!" equ "=====" (
    set /a out_file_counter=out_file_counter+1

 ) else (
   echo !line!>>out.file.!out_file_counter!
  )
)
endlocal