Reputation: 23
I need to take a known number of lines from one text doc and put them in another. During or after this process I need to look at certain columns only. My idea:
setlocal enabledelayedexpansion
FOR /F "SKIP=21 TOKENS 1,3 DELIMS= " %%B IN (INPUT.TXT) DO ECHO %%B %%C > OUTPUT.TXT
When I try this I just get the last line of the file printed. I eventually want just lines 22-34, 1st and 3rd columns. Please keep simple.
Upvotes: 1
Views: 929
Reputation: 130819
It is more efficient to enclose entire construct in parens and redirect only once.
You can intentionally divide by zero and detect when to quit without any need for delayed expansion.
@echo off
setlocal disableDelayedExpansion
set "cnt=13"
>output.txt (
for /f "skip=21 tokens=1,3 delims= " %%B in (input.txt) do (
echo(%%B %%C
set /a "1/(cnt-=1)" 2>nul || goto :break
)
)
:break
Or, if you get my JREPL.BAT regular expression text processing utility, then all you need is the following from the command line:
jrepl "^([^ ]*) [^ ]* ([^ ]*)" "$1 $2" /jmatch /jbegln "skip=(ln<22 || ln>34)" /f input.txt /o output.txt
The above assumes there is exactly one space between tokens. The regular expression can be modified if there may be multiple spaces between tokens.
You must use CALL JREPL if you use the command within a batch script.
Upvotes: 0
Reputation: 57252
setlocal enabledelayedexpansion
set /a counter=32-21
FOR /F "SKIP=21 TOKENS 1,3 DELIMS= " %%B IN (INPUT.TXT) DO (
ECHO %%B %%C
set /a counter=counter-1
if !counter! == 0 (
goto :break
)
)>> OUTPUT.TXT
:break
Like is posted you script will print all lines after 21th.To stop at 34 you'll need a break condition. And as Kevin pointed you need appending redirection.
Upvotes: 0