My problem: I have a .txt
file of strings I'd like to sort through and label in the range 1 through 4. I only want to keep items 1 and 2 while deleting 3 and 4. I know I can use %4
to keep the 0 and 1 tokenized items while deleting 2 and 3, but the trouble is using an if statement while modulating, and doing that over the whole file. The file size will be at least 5000 /n lines long.
Here's the code I've had the most success with so far. I'm using the code I found in the top comment here as a base.
@echo on
for /f "tokens=*" %%a in ('findstr /n .* "C:\...\TEXTFILE.txt"') do (
set "FullLine=%%a"
for /f "tokens=1* delims=:" %%b in ("%%a") do (
setlocal enabledelayedexpansion
set "LineData=!FullLine:*:=!"
set /a MODVAR = %%b%%2
if %%b == !MODVAR! >"TEXTFILE.txt" echo(!LineData!
endlocal
)
)
pause
This replaces only the text from line 1. I know I said I want line 1 and 2, but I imagine I'd use an else
statement to get that second line as well. Just need the current code to work before I try that problem.
I've read that it would be easier to use a regex find and replace, but I'd much prefer to find a solution using batch so I can write (get help with) the current code and be done. Rather than copying/pasting possibly multiple times.
Edit: an example of my initial text document is:
aaa
bbb
ccc
ddd
eee
(and so on) where i want to only keep aaa, bbb, and eee