windows - Split a text file if two consecutive lines almost identical -
i need split text file (using .bat command) based on content of string of previous line (from position 2 13) , content of string of current line (from position 2 13)...
i explain:
my file looks that:
ia1234567890a xx33 aze bla1 xx34 des bla2 xx34 des bla3 xx34 des fa1234567890a xx35 aze ia1234567890a xx36 aze bla4 xx34 des bla5 xx34 des bla6 xx34 des fa1234567890a xx37 aze ib0987654321a xx38 aze bla7 xx34 des bla8 xx34 des bla9 xx34 des fb0987654321a xx39 aze i want split file when first 12 characters of 1 line starting "i" (without taking account "i") different first 12 characters of previous line (which starting "f" except first line, comparison should not take account "f").
so not split file between these 2 lines:
fa1234567890a xx35 aze ia1234567890a xx36 aze but split file between these 2 lines:
fa1234567890a xx37 aze ib0987654321a xx38 aze i know how split file using delimiter, totally lost comparison thing...
i appreciate if 1 of me of tricky case...
thanks!
if input file large, method should run faster because not check all lines. correctly process lines special batch characters.
@echo off setlocal enabledelayedexpansion rem read first line, , create dummy previous "endline" same name set /p "endname=" < test.txt set "endname=f%endname:~1%" set startline=1 set "startname=" rem redirect input file code block, in order read < test.txt ( rem locate lines start "i" or "f" /f "tokens=1,2 delims=: " %%a in ('findstr /n /b "i f" test.txt') ( if not defined startname ( set "startname=%%b" if "!startname:~1,12!" neq "!endname:~1,12!" ( rem new section starts: copy own file set /a lines=endline-startline+1 (for /l %%i in (1,1,!lines!) ( set /p "line=" echo !line! )) > "part !endname:~1,12!.txt" set "endname=f%startname:~1%" set "startline=%%a" ) ) else ( set "endline=%%a" set "endname=%%b" set "startname=" ) ) rem copy last section own file findstr "^" > "part !endname:~1,12!.txt" ) output:
c:\> type part*.txt part a1234567890a.txt ia1234567890a xx33 aze bla1 xx34 des bla2 xx34 des bla3 xx34 des fa1234567890a xx35 aze ia1234567890a xx36 aze bla4 xx34 des bla5 xx34 des bla6 xx34 des fa1234567890a xx37 aze part b0987654321a.txt ib0987654321a xx38 aze bla7 xx34 des bla8 xx34 des bla9 xx34 des fb0987654321a xx39 aze
Comments
Post a Comment