Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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 EnableDelayedExpansionfor %%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 helpmax

@echo off
setLocal EnableDelayedExpansionfor /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

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

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

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

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 gawkthank you anyway for your contribution, I'll study it, I'll promise it...

![]() |
![]() |
![]() |

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