Blue Fish <(E-Mail Removed)> wrote:
> May I have idea how can I remove the line and one line above if the
> char. was matched?
You might wish to study and run the following example code
@echo off & setlocal enableextensions
::
:: Make a test file
set testfile=C:\_M\MyTest.txt
for %%f in ("%testfile%") do if exist %%f del %%f
for /L %%i in (1,1,3) do echo This is row 0%%i>>"%testfile%"
echo.>>"%testfile%"
for /L %%i in (5,1,9) do echo This is row 0%%i>>"%testfile%"
rem type "%testfile%"
::
set targetString=row 07
::
:: Get the line number of the target string
for /f "tokens=1 delims=:" %%a in ('
findstr /n /c:"%targetString%" "%testfile%"') do (
set lineNber=%%a)
::
set /a n1=%lineNber%-2
sed -n 1,%n1%p "%testfile%"
set /a n2=%lineNber%+1
sed -n %n2%,$p "%testfile%"
::
for %%f in ("%testfile%") do if exist %%f del %%f
endlocal & goto :EOF
The test file is
This is row 01
This is row 02
This is row 03
This is row 05
This is row 06
This is row 07
This is row 08
This is row 09
The output is
This is row 01
This is row 02
This is row 03
This is row 05
This is row 08
This is row 09
The solution avoids the empty line catch common in pure batches.
For more information and alternatives see e.g.
23} How do I get the n'th, the first and the last line of a text file?
file:///C:/_G/WWW/~NETIKKA/SALMTI02/INFO/tscmd023.htm
All the best, Timo
--
Prof. Timo Salmi ftp &
http://garbo.uwasa.fi/ archives 193.166.120.5
Department of Accounting and Business Finance ; University of Vaasa
private.php?do=newpm&u= <http://www.uwasa.fi/~ts/> ; FI-65101, Finland
Useful CMD script tricks
http://www.netikka.net/tsneti/info/tscmd.htm