Page 1 of 2

Having for loop reference findstr result, moving two lines d

Posted: 22 Mar 2013 01:11
by pditty8811
I have two codes but part5 is grabbing the wrong variable in a different file than part4.

part4 is using var4, and part5 var5. I want var5 to be grabbed 2 lines down "skip=2 tokens=2 delims==" but for some reason I cannot skip lines when referencing the findstr in part5. Part5 is all that needs changing, part4 is good to go.

**So in essense, How do I get part5 to grab a variable two lines below part4's findstr?**


Code: Select all

    SETLOCAL ENABLEDELAYEDEXPANSION

    :part4
    set var4=
    findstr /m "Ship sunk!" "C:\Users\P Ditty\Documents\SH3\data\cfg\Backups_SCR2\*.clg" "C:\Users\P         Ditty\Documents\SH3\data\cfg\Backups_SCR2\Log_*.cfg"
    if not errorlevel 1 (
    for /f "tokens=4 delims==,|" %%D in ('findstr "Ship sunk!" "C:\Users\P Ditty\Documents\SH3       \data\cfg\Backups_SCR2\*.clg" "C:\Users\P Ditty\Documents\SH3\data\cfg\Backups_SCR2\Log_*.cfg"') do (
    set var4=!var4!%%D
     goto :eof
    )
    )

    :part5
    findstr "Ship sunk!" "C:\Users\P Ditty\Documents\SH3\data\cfg\Backups_SCR2\*.clg" "C:\Users\P  Ditty\Documents\SH3\data\cfg\Backups_SCR2\Log_*.cfg"
    if not errorlevel 1 (
    for /f "skip=2 tokens=2 delims==" %%E in ('findstr "Ship sunk!" "C:\Users\P Ditty\Documents\SH3\data\cfg\Backups_SCR2\*.clg" "C:\Users\P Ditty\Documents\SH3\data\cfg\Backups_SCR2\Log_*.cfg"')  do     (
    set var5=
    set var5=!var5!%%E
    goto :eof
    )
    )
   

Re: Having for loop reference findstr result, moving two lin

Posted: 22 Mar 2013 01:37
by mfm4aa
You can count the lines, add 2 and use "skip" or "more" or "findstr" again.

Re: Having for loop reference findstr result, moving two lin

Posted: 22 Mar 2013 02:39
by pditty8811
I'm trying to figure out how to do that. This is what I have:


Code: Select all

set /a maxlines5=2
set /a linecount5=0
findstr /c:"Ship sunk" "C:\Users\P Ditty\Documents\SH3\data\cfg\Backups_SCR2\*.clg" "C:\Users\P Ditty\Documents\SH3\data\cfg\Backups_SCR2\Log_*.cfg"
if not errorlevel 1 (
for /f "tokens=2 delims==" %%E in ('findstr /c:"Ship sunk" "C:\Users\P Ditty\Documents\SH3\data\cfg\Backups_SCR2\*.clg" "C:\Users\P Ditty\Documents\SH3\data\cfg\Backups_SCR2\Log_*.cfg"') do (
set var5=!var5!%%E
goto loop5a

)
:loop5a
set /a linecount5+=1
if !linecount5! GEQ %maxlines5% goto part5a
)
:part5a
echo !var5!
pause

Re: Having for loop reference findstr result, moving two lin

Posted: 22 Mar 2013 13:22
by pditty8811
Could someone please show me an example of counting the lines below a findstr, to enact a for loop. I need to change a line two lines below a found string, using a for loops.

Re: Having for loop reference findstr result, moving two lin

Posted: 22 Mar 2013 14:59
by mfm4aa
One example without DelayedExpansion:

Code: Select all

@echo off &setlocal
set "search=mysearch"

:: find first %search%
set "count="
for /f "tokens=1*delims=:" %%i in ('findstr /n "%search%" file.txt') do if not defined count set /a count=%%i
echo pattern:  [%search%] found at line %count%
:: two lines down --> skip+1
set /a count+=1
set "variable="
set "found="
:: grab line+2
for /f "skip=%count%tokens=1*delims=:" %%i in ('findstr /n "^" file.txt') do if not defined found set "found=%%i"&set "variable=%%j"
:: Note: you need %found% to find empty lines
echo variable: [%variable%] found at line %found%
endlocal



EDIT: we need two lines down ...

Re: Having for loop reference findstr result, moving two lin

Posted: 22 Mar 2013 15:47
by pditty8811
Is %search% the variable two lines above, or the variable two lines below?

Re: Having for loop reference findstr result, moving two lin

Posted: 22 Mar 2013 15:56
by pditty8811
Hard time getting this to work. Can you add my tokens and delims into your code?

Re: Having for loop reference findstr result, moving two lin

Posted: 22 Mar 2013 18:33
by pditty8811
mfm4aa wrote:One example without DelayedExpansion:

Code: Select all

@echo off &setlocal
set "search=mysearch"

:: find first %search%
set "count="
for /f "tokens=1*delims=:" %%i in ('findstr /n "%search%" file.txt') do if not defined count set /a count=%%i
echo pattern:  [%search%] found at line %count%
:: two lines down --> skip+1
set /a count+=1
set "variable="
set "found="
:: grab line+2
for /f "skip=%count%tokens=1*delims=:" %%i in ('findstr /n "^" file.txt') do if not defined found set "found=%%i"&set "variable=%%j"
:: Note: you need %found% to find empty lines
echo variable: [%variable%] found at line %found%
endlocal



EDIT: we need two lines down ...



How to loop this so it searches for the variable more than once in the same file, tell me what is two lines below it every time?

Re: Having for loop reference findstr result, moving two lin

Posted: 23 Mar 2013 00:07
by mfm4aa
Do you mean "find last search string"? What have you tried?

Re: Having for loop reference findstr result, moving two lin

Posted: 23 Mar 2013 00:19
by pditty8811
I'm figuring it out.

Now I'm running into an issue of a previous variable loosing value when a new loop occurs. Why, when I assign a variable and then goto :eof, then try to find that variable I am getting "Echo is off"?

Re: Having for loop reference findstr result, moving two lin

Posted: 23 Mar 2013 00:22
by pditty8811
I'm using enabledexpansions.

Re: Having for loop reference findstr result, moving two lin

Posted: 23 Mar 2013 00:35
by mfm4aa
Echo is off --> variable is empty or contains only spaces/tabs.

Re: Having for loop reference findstr result, moving two lin

Posted: 23 Mar 2013 08:06
by mfm4aa
Search in a file and find lines up or down (e.g. 2 lines up-->updown=-2 / 2 lines down-->updown=2):

Code: Select all

@echo off &setlocal
set "fname=file.txt"
set "search=mysearch"
set /a updown=2

set "skip0="
set /a skipcount=0
:findloop
set "count="
for /f "%skip0%tokens=1*delims=:" %%i in ('findstr /n "%search%" "%fname%"') do if not defined count set /a count=%%i
if defined count (echo Pattern:  [%search%] found at line %count% in %fname%) else echo No more [%search%] found in %fname%&goto:eof
if %updown% neq 0 (set /a skip1=count+updown-1) else echo Nothing to do!&goto:eof
set /a skipcount+=1
set "skip0=skip=%skipcount%"
if %skip1% leq 0 echo Target line below zero&goto:findloop
set "variable="
set "found="
for /f "skip=%skip1%tokens=1*delims=:" %%i in ('findstr /n "^" "%fname%"') do if not defined found set "found=%%i"&set "variable=%%j"
if defined found (echo Variable: [%variable%] found at line %found%) else echo Target line after EOF
goto:findloop
endlocal


It doesn't use DelayedExpansion & is rather slow. Patches welcome.

Re: Having for loop reference findstr result, moving two lin

Posted: 23 Mar 2013 09:22
by foxidrive
Here's another method that doesn't use delayed expansion.

The search string is expected on column one but is case insensitive.
Be aware that lines starting with : characters will be corrupted.


Code: Select all

@echo off
set "fname=file.txt"
set "search=apple"
set upORdown=2

(
echo one two three
echo.four five ^& six
echo.apple orange pear
echo.seven eight nine
echo.ten eleven twelve
echo.0 0 0 0
echo.1 2 3
echo.4 5 6
echo.apple skateboard
echo.7 8 9
echo.10 11 12
echo.aa bb cc
echo.dd ee ff
echo.gg hh ii
)> "%fname%"

findstr /n "^" "%fname%" >"%fname%.2"

for /f "tokens=1,* delims=:" %%a in (' findstr /n /i "^%search%" "%fname%" ') do call :getstring %%a
del "%fname%*"
pause
goto :EOF

:getstring
set /a num=%1+upORdown
for /f "tokens=1,* delims=:" %%c in (' findstr "^%num%:" "%fname%.2" ') do echo This string "%%d" on line [%%c] is %upORdown% lines from "%search%" on line [%%a]

Re: Having for loop reference findstr result, moving two lin

Posted: 23 Mar 2013 09:40
by mfm4aa
Really nice and fast :)