Batch - Extract needed lines from files

January 7, 2010 at 11:16:23
Specs: Windows XP
I have from 3 to 5 files in which i need search to.
Its names 1.txt 2.txt 3.txt 4.txt 5.txt

each file has from 2000 to 3000 lines

How check how many files with extension .txt in folder?

from 1.txt extract lines from 2 to 10, from 300 to 500 and from 1000 to 2000
next exstract the same lines frome 2.txt 3.txt and etc.

and then merge extracted lines in file test.txt by order from 1.txt to 5.txt


See More: Batch - Extract needed lines from files

Report •

#1
January 7, 2010 at 13:05:25
@echo off > test.txt & setLocal EnableDELAYedeXpansion

set T=

for /f "tokens=* delims= " %%a in ('dir/b/on ?.txt') do (
  set /a T+=1
  if exist !T! del !T!
  set N=
for /f "tokens=* delims= " %%i in (%%a) do (
  set /a N+=1
    if !N! geq 2    if !N! leq 10   >> !T! echo %%i
    if !N! geq 300  if !N! leq 500  >> !T! echo %%i
    if !N! geq 1000 if !N! leq 2000 >> !T! echo %%i
)
)

echo there are !T! txt files

for /L %%f in (1 1 !T!) do (
  type %%f >> test.txt
)

for /L %%f in (1 1 !T!) do (
  del %%f
)


=====================================
Helping others achieve escape felicity

M2


Report •

#2
January 7, 2010 at 22:58:34
Thansks for reply!
i will try it today. Where I can read about batch programing?
I programing in VBA and there are litlle differ.
Exact I need understand what to do following variables:
tokens
delims
/f its like "for i = " in VBA ?
set /a T+=1 - in VBA must be "set a = T | t=t+1 " or not?
set N=
%%Letter - ? it most

Report •

#3
January 8, 2010 at 06:26:26
The best thing to me about batch scripting is you can learn right
from the command line.

Want to know about tokens and delims? Go to cmd and type

for /?

Variables in for loops are like %i if you run the for command from
the command line, but %%i within a batch file.


Report •

Related Solutions

#4
January 8, 2010 at 07:06:42
That code doesnt work, in each file between lines exist many blank spaces. How count spaces like line number? How to find needed start text in lines between 900 and 930? and write to file test.txt lines between 900 and 930 starting whith my text and ending whith my text? It must be something like find text block in interval. It can start on 901 or 905 and ends on 921 or 928

Report •

#5
January 8, 2010 at 11:27:07
rawas: the easiest way i can think of is to convert the blank lines to a space in your input files, but i don't know if your outputfile is allowed to have a space instead of a null line. then you can use M2go's code exactly as it stands.
this will convert the inputfiles:

@echo off & setlocal enabledelayedexpansion
for %%b in (?.txt) do (
set outf=%%~nb.tmp
@echo off>outf
for /f %%a in ('find /c /v "" ^< list') do set /a lc=%%a
call :spc<list
move /y !outf! %%b
)
call Mech2go.bat
goto :eof

:spc
for /l %%i in (1 1 %lc%) do (
set xx=
set /p xx=
if "!xx!"=="" set xx=
>>!outf! echo.!xx!)

but note: your .txt files are overwritten and your outputfile will have one space on each blank line. These can be removed, and the .txt files don't have to be overwritten if you change the filename used by M2go's code (from .txt to .tmp) and take out the "move /y" statement from my code.

oops, sorry, some of your last msg didn't "soak in" to my brain, (about looking for trigger-strings to initiate the output stream).
i will try to re-work it some...


Report •

#6
January 8, 2010 at 15:33:42
This will accomodate the blank lines.

====================================

@echo off > test.txt & setLocal EnableDELAYedeXpansion

set T=

for /f "tokens=* delims= " %%a in ('dir/b/on ?.txt') do (
  set /a T+=1
  if exist !T! del !T!
  set N=
for /f "tokens=1* delims=[]" %%i in ('find /v /n "" ^< %%a') do (
  set /a N+=1
    if !N! geq 2    if !N! leq 10   >> !T! echo.%%j
    if !N! geq 300  if !N! leq 500  >> !T! echo.%%j
    if !N! geq 1000 if !N! leq 2000 >> !T! echo.%%j
)
)

echo there are !T! txt files

for /L %%f in (1 1 !T!) do (
  type %%f >> test.txt
)

for /L %%f in (1 1 !T!) do (
  del %%f
)


=====================================
Helping others achieve escape felicity

M2


Report •

#7
January 9, 2010 at 05:35:24
Please comment that to do each string in your code, because the code is working but not exactly like i need, und i cant see russian simbols in saved file/

Report •

#8
January 9, 2010 at 07:24:14
i cant see russian simbols in saved file
This is to be expected; the above lines involve text operations, and CMD is an ASCII environment, not Unicode.

Report •

Ask Question