View Full Version : Need batch file - Ironbits
em99010pepe
06-29-2008, 11:33 AM
Ironbits,
Please help me here. In a txt file I just want to retrieve the lines every 3 lines (beginning the counting from the third line) to a new txt file. Example:
file.txt content
line1: text1
line2: text2
line3: text3
line4: text4
line5: text5
line6: text6
So the new txt file will have
newfile.txt
text3
text6
Is it possible to do this? I think the program should read the file.txt until it ends and retrieve the lines to a new txt file.
Carlos
gopher_yarrowzoo
06-29-2008, 01:01 PM
I don't think it's possible with a dos batch file maybe with linux or small php / perl script that can basically do
$fhand=fopen($filename,"r");$count=1;$fout=fopen($filenameout,"w");
while (!feof($fhand)) { $line=fgets($fhand); if ($count==3) { fwrite($fout,$line); $count=1;} else { $count++; } } fflush($fout); fclose($fout);fclose($fhand);
that php script might just work no idea not tested it just psuedo coded.
IronBits
06-29-2008, 04:04 PM
Ironbits,
Please help me here. In a txt file I just want to retrieve the lines every 3 lines (beginning the counting from the third line) to a new txt file. Example:
file.txt content
line1: text1
line2: text2
line3: text3
line4: text4
line5: text5
line6: text6
So the new txt file will have
newfile.txt
text3
text6
Is it possible to do this? I think the program should read the file.txt until it ends and retrieve the lines to a new txt file.
Carlos Sure, I assume you know something about scripting, feel free to modify to taste.
it reads a file called input.txt
it writes a file called output.txt
works in Windows XP, not sure if an older version of DOS can handle ENABLEDELAYEDEXPANSION
@echo off
:: Filename: Count.bat
:: Purpose: outputs every 3rd line
setlocal ENABLEDELAYEDEXPANSION
set /a count=0
for /f "tokens=*" %%a in ('type "input.txt"') do (
set /a count=!count! + 1
if !count! equ 3 echo %%a >> "output.txt" & echo %%a & set /a count=!count! -3
)
ENDLOCAL
:EOF
-----------------------------------------
output looks like this
line3: text3
line6: text6
line9: text9
line12: text12
line15: text15
em99010pepe
06-29-2008, 05:09 PM
IronBits,
That worked, thanks a bunch! Now one last thing, it should sort the lines in the output.txt. The lines are always like this:
145*2^640005-1 is not prime. Res64: 3395BF4EEAF1BE2D Time : 376.0 sec
I suppose it must read the line until it reaches -1, then sort by number...maybe only with perl. Anyway, I can manually check if a number is missing in case it is not possible to do this.
Once again thank you very much.
Carlos
IronBits
06-29-2008, 05:51 PM
sort /rec 15 output.txt /o newfile.txt
or
sort /rec 15 output.txt to see if it's doing the sort the way you want
or another way is to start the sort at column 3 and only compare up to column 15
sort /+3 /rec 15 output.txt > newfile.txt ;)
Powered by vBulletin® Version 4.2.4 Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.