Solved Batch find in text

Hewlett-packard / Pavilion touchsmart amd a...
January 26, 2014 at 15:43:42
Specs: Windows 7, 8GB
I need a batch file to delete all the lines of text in a file, tmp.txt , up to a specific string. Then I need it to also delete everything after 4 lines after the string.. For example if tmp.txt has:
line1
line2
line3
mystring
line4
line5
line6
line7
line8
line9

I want to have
mystring
line4
line5
line6
line7


See More: Batch find in text

Report •


#1
January 26, 2014 at 22:28:49
✔ Best Answer
@echo off & setlocal enabledelayedexpansion
set c=0
(for /f "tokens=*" %%a in (file.txt) do (
if /i "%%a" equ "my string" set /a c+=1

if !c! gtr 5 goto :eof
if !c! gtr 0 (
echo %%a
set /a c+=1
)
)
)>newfile
::======== end script

@M2: Ha ha! hopefully at least one of the scripts will work ;)

message edited by nbrane


Report •

#2
January 27, 2014 at 02:06:36
::====== script starts here ===============
::
:: plus4.bat 2014-01-27 15:23:50.83
@echo off > NEWFILE & setLocal enableDELAYedeXpansioN

call :sub1
set/a T=H+4

:main
set N=
for /f "tokens=* delims= " %%a in (myfile) do (
set/a N+=1
if !N! geq !H! if !N! leq !T! echo.%%a
) >> NEWFILE
goto :eof

:sub1
for /f "tokens=1 delims=[]" %%i in ('find /n "mystring" ^< myfile') do (
set/a H=%%i
)
goto :eof
::====== script ends here =================

=====================
M2 Golden-Triangle


Report •

#3
January 27, 2014 at 02:09:38
ืิnbrane

oops, took too long

didn't refresh

=====================
M2 Golden-Triangle


Report •
Related Solutions


Ask Question