Computing.Net > Forums > Programming > batch extract lines from files

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

batch extract lines from files

Reply to Message Icon

Name: maxbre
Date: July 29, 2008 at 04:01:41 Pacific
OS: win xp
CPU/Ram: 3
Comment:

hi all

I have a bunch of txt files from which I need to extract different set of lines:
from line 1 to line 3 to be stored in a single comprehensive file called “headings.txt”
and
from line 4 to line 58 to be stored in different separate files called *new.txt (where * stays for the original filename)

I’m stuck because I do not know how to combine this commands within the same cycle structure of a single batch

please have a look on this awful stuff

::>>>>>>>>>>>>>>>>
@echo off
setLocal EnableDelayedExpansion

for %%A in (originali\*.txt) do (
set lineNUM=1
set filetxt=%%~nA
for /f "skip=3 tokens=* delims=" %%L in (%%A) do (call :keeper %%L)
)
Set lineNUM=
Set filetxt=

GoTo :EOF

:keeper
if %lineNUM% leq 3 echo %*>>headings.txt
::the next line is not working together with the previous one: how about to combine them?
::if %lineNUM% leq 51 echo %*>>%filetxt%new.txt
set /a lineNUM+=1
)

GoTo :EOF
::>>>>>>>>>>>>>>>>


Thanks again for your help

max



Sponsored Link
Ads by Google

Response Number 1
Name: Mechanix2Go
Date: July 29, 2008 at 04:57:19 Pacific
Reply:

@echo off
setLocal EnableDelayedExpansion

for /f "tokens=* delims= " %%a in ('dir/b/a-d') do (
set outfile=%%~Nanew.txt
set /a N=0
for /f "tokens=* delims= " %%i in (%%a)do (
set /a N+=1
if !N! geq 1 if !N! leq 3 echo %%i >> headings.txt
if !N! geq 4 if !N! leq 58 echo %%i >> !outfile!
)
)


=====================================
If at first you don't succeed, you're about average.

M2


0

Response Number 2
Name: maxbre
Date: July 29, 2008 at 05:41:36 Pacific
Reply:

thank you so much m2
a very neat solution: now I realise I was stuck (also) because I've frozen variables with % instead of using ! as you wisely suggested;
after some slight modifications, among many other small things to adjust I needed to discard first 3 lines, your input worked perfectly!

bye
max


0

Response Number 3
Name: Mechanix2Go
Date: July 29, 2008 at 05:55:45 Pacific
Reply:

hi max,

Yeah, I wondered if you wanted the first 3 in headers more than once. But I've learned the hard way not to second guess people; rather just try to answer the question.


=====================================
If at first you don't succeed, you're about average.

M2


0

Response Number 4
Name: ghostdog
Date: July 30, 2008 at 04:54:04 Pacific
Reply:

if you can use gawk, download from here:
http://gnuwin32.sourceforge.net/pac...


NR<3 { print > "headings.txt"}
NR>=4 && NR<=58 {print > "new.txt"}

save the above as script.awk and on command line

c:\test> gawk -f script.awk file


0

Response Number 5
Name: maxbre
Date: July 30, 2008 at 06:51:10 Pacific
Reply:

hi gostdog
with no doubt your solution is really neat, I would say it is cristal clear, but I'm not jet much profcient in gawk

thank you anyway for your contribution, I'll study it, I'll promise it...


0

Related Posts

See More



Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: batch extract lines from files

batch to extract rows from files www.computing.net/answers/programming/batch-to-extract-rows-from-files/15625.html

Extract Lines from a Text File www.computing.net/answers/programming/extract-lines-from-a-text-file/13817.html

batch extract words from line www.computing.net/answers/programming/batch-extract-words-from-line/17326.html