Solved Split one file into multiple files

January 27, 2012 at 19:30:54
Specs: Win 7 Pro, 4.17Ghz 8GB ram
I was wondering if there is a way in batch to remove 7 lines from a text file and echo them into a different text file

ex
input.txt:
id=12345678
x=234
y=674
z=842
avg=746
c=924
id=1234984
x=235
y=673
z=848
avg=741
c=927
id=123498464
x=2355
y=6738
z=8489
avg=7415
c=9274

then it would make
output1.txt:
id=12345678
x=234
y=674
z=842
avg=746
c=924

output2.txt:
id=1234984
x=235
y=673
z=848
avg=741
c=927

output3:
id=123498464
x=2355
y=6738
z=8489
avg=7415
c=9274

whats the best way to do that
I was thinking just take anything from the id,x,y,z,avg, and c and put it to a text file but that would just basically copy the file because there are multiple id,x,y,z,avg, and c's

any suggestions?



See More: Split one file into multiple files

Report •


#1
January 27, 2012 at 21:04:52
I think as long as that file stays static like that it shouldn't be much of an issue. a FOR loop with an incremented variable should do it. I'll work on something and post it.

Report •

#2
January 27, 2012 at 21:14:15
@setlocal enableextensions enabledelayedexpansion
@echo off
set input_file=lines.txt
cls
set /a out_num=1
set /a line1=1
set /a line2=2
set /a line3=3
set /a line4=4
set /a line5=5
set /a line6=6
set /a times=0

:start_sep
if times==4 (
echo done
pause
exit

) else (
set /a times+=1
call :start_find
call :add_num_line
)

call :start_find
call :add_num_line
call :start_find
pause
exit

:start_find
cls
echo.
echo %out_num%
echo.
echo %line1%
echo %line2%
echo %line3%
echo %line4%
echo %line5%
echo %line6%
set lines=%line1% %line2% %line3% %line4% %line5% %line6%
set curr=1
for /f "delims=" %%a in ('type %input_file%') do (
for %%b in (!lines!) do (
set !curr!=curr
if !curr!==%%b echo %%a >>output_%out_num%.txt
)
set /a "curr = curr + 1"
)
endlocal
goto :eof
:add_num_line
set /a out_num+=1
set /a line1+=6
set /a line2+=6
set /a line3+=6
set /a line4+=6
set /a line5+=6
set /a line6+=6
goto :eof
pause

this is what i came up with.

and seems to be working

sorry for posing the question and then answering it my self

but im open to suggestions on how to do it easier/cleaner


Report •

#3
January 28, 2012 at 00:31:56
✔ Best Answer
Please test this batch script, it seems to work using the input file you provided.

@echo off
cls
setlocal enabledelayedexpansion

for /f "tokens=*" %%1 in (input.txt) do (
    set inline=%%1
    
    if /i "!inline:~0,2!" equ "id" (set /a ctr+=1
       set outfile=output!ctr!.txt
       )

       echo !inline!>>!outfile!
)

dir output*.txt


Please come back & tell us if your problem is resolved.


Report •

Related Solutions

#4
January 28, 2012 at 00:49:59
Oh wow that works way better than the way i did it.

Im confused by it though

how does it know to only grab those 7 lines?

Can you explain it to me please

I have similar things like this and i think understanding why this works would help me out with the rest


Report •

#5
January 28, 2012 at 09:46:32
It increments the ctr variable every time the first two characters of the FOR loop input matches "id". This causes the loop to make a new output file every time it sees that pattern, and "id" occurs every 7th line.

Report •

#6
January 29, 2012 at 21:38:51
Thanks for the reply but i have one more question

what is the ~0,2! for?


do you know of any online reference that can i can read that will teach me all i need to know about for statements and tokens?


Report •

#7
January 29, 2012 at 23:58:46
To extract a two-character string without skipping any characters from the environment variable Inline. In this case the first two characters of the environment variable Inline are extracted. Enter Set /? at the command prompt for more detail.

Enter FOR ?/ at the command prompt to view For loop specs. There is a fairly comprehensive list of Win commands at http://ss64.com/nt/

Good luck.

Wahine.


Please come back & tell us if your problem is resolved.


Report •

#8
January 30, 2012 at 00:12:46
Thank you so much for your help!

I really appreciate it


Report •

#9
January 30, 2012 at 05:33:47
Is this the part where someone points to Microsoft's documentation?

How To Ask Questions The Smart Way


Report •

#10
March 26, 2012 at 21:45:38
I know this post is kinda solved but I just ran into a problem with the script

when I run it the output of the last item it finds has the last item plus everything thats after it

my script:

@echo off
cls
set id=06010101
setlocal enabledelayedexpansion

for /f "tokens=*" %%1 in (input.txt) do (
    set inline=%%1
    
    if /i "!inline:~0,17!" equ "TrodeID= %id%" (set /a ctr+=1
       set outfile=%id%_!ctr!.txt
       )

       echo !inline!>>!outfile!
)

dir output*.txt

pause


the output of the third one found:

TrodeID= 06010101-3
[Offsets]
X= 0.0007
Y= -0.0012
Z= -0.0006
Avg= -0.0054
C= 0.0031
TrodeID= 06010201-1
[Offsets]
X= 0.0004
Y= 0.0001
Z= -0.0003
Avg= -0.0054
C= 0.0003
TrodeID= 06010201-2
[Offsets]
X= 0.0004
Y= -0.0003
Z= -0.0002
Avg= -0.0056
C= 0.0000
TrodeID= 06010201-3
[Offsets]
X= 0.0003
Y= -0.0004
Z= -0.0001
Avg= -0.0056
C= -0.0007
TrodeID= 06010203-1
[Offsets]
X= 0.0002
Y= -0.0005
Z= -0.0004
Avg= -0.0048
C= 0.0009

the input:
<pre>
TrodeID= 06010101-1
[Offsets]
X= 0.0003
Y= -0.0009
Z= -0.0003
Avg= -0.0054
C= -0.0007

TrodeID= 06010101-2
[Offsets]
X= 0.0004
Y= -0.0008
Z= 0.0001
Avg= -0.0056
C= -0.0024

TrodeID= 06010101-3
[Offsets]
X= 0.0007
Y= -0.0012
Z= -0.0006
Avg= -0.0054
C= 0.0031

TrodeID= 06010201-1
[Offsets]
X= 0.0004
Y= 0.0001
Z= -0.0003
Avg= -0.0054
C= 0.0003

TrodeID= 06010201-2
[Offsets]
X= 0.0004
Y= -0.0003
Z= -0.0002
Avg= -0.0056
C= 0.0000

TrodeID= 06010201-3
[Offsets]
X= 0.0003
Y= -0.0004
Z= -0.0001
Avg= -0.0056
C= -0.0007

TrodeID= 06010203-1
[Offsets]
X= 0.0002
Y= -0.0005
Z= -0.0004
Avg= -0.0048
C= 0.0009



Report •

#11
March 27, 2012 at 01:13:11
This works with the input from your last post.

@echo off
setlocal enabledelayedexpansion
set #=0
set _=0
for /f "tokens=*" %%i in (
'type %1'
) do (
set /a _+=1
if !_! leq 6 (
>>newfile!#!.txt echo %%i
) else (
>>newfile!#!.txt echo %%i
set /a #+=1
set _=0
)
)

Tony


Report •

#12
March 27, 2012 at 17:34:12
im not quite sure i know how to use that? where is the input file and where does it search for the id?

Report •

#13
March 27, 2012 at 20:03:45
You run it as a command line utility:

script.bat input.txt

It doesn't search for the ID. It just splits the file every 7 lines. Are your input files always going to be like this?

TrodeID= 06010201-1
[Offsets]
X= 0.0004
Y= 0.0001
Z= -0.0003
Avg= -0.0054
C= 0.0003

That is 7 lines, so my script just splits the file every 7 lines.

Tony


Report •

#14
March 27, 2012 at 20:17:52
It's supposed to be every 7 lines but some times it gets messed up so that won't work always.

Report •


Ask Question