Discussion forum for all Windows batch related topics.
Moderator: DosItHelp
-
pditty8811
- Posts: 184
- Joined: 21 Feb 2013 15:54
#1
Post
by pditty8811 » 22 Mar 2013 01:11
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
)
)
-
mfm4aa
- Posts: 70
- Joined: 13 Feb 2013 14:02
- Location: Europe
#2
Post
by mfm4aa » 22 Mar 2013 01:37
You can count the lines, add 2 and use "skip" or "more" or "findstr" again.
-
pditty8811
- Posts: 184
- Joined: 21 Feb 2013 15:54
#3
Post
by pditty8811 » 22 Mar 2013 02:39
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
-
pditty8811
- Posts: 184
- Joined: 21 Feb 2013 15:54
#4
Post
by pditty8811 » 22 Mar 2013 13:22
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.
-
mfm4aa
- Posts: 70
- Joined: 13 Feb 2013 14:02
- Location: Europe
#5
Post
by mfm4aa » 22 Mar 2013 14:59
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 ...
-
pditty8811
- Posts: 184
- Joined: 21 Feb 2013 15:54
#6
Post
by pditty8811 » 22 Mar 2013 15:47
Is %search% the variable two lines above, or the variable two lines below?
-
pditty8811
- Posts: 184
- Joined: 21 Feb 2013 15:54
#7
Post
by pditty8811 » 22 Mar 2013 15:56
Hard time getting this to work. Can you add my tokens and delims into your code?
-
pditty8811
- Posts: 184
- Joined: 21 Feb 2013 15:54
#8
Post
by pditty8811 » 22 Mar 2013 18:33
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?
-
mfm4aa
- Posts: 70
- Joined: 13 Feb 2013 14:02
- Location: Europe
#9
Post
by mfm4aa » 23 Mar 2013 00:07
Do you mean "find last search string"? What have you tried?
-
pditty8811
- Posts: 184
- Joined: 21 Feb 2013 15:54
#10
Post
by pditty8811 » 23 Mar 2013 00:19
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"?
-
mfm4aa
- Posts: 70
- Joined: 13 Feb 2013 14:02
- Location: Europe
#12
Post
by mfm4aa » 23 Mar 2013 00:35
Echo is off --> variable is empty or contains only spaces/tabs.
-
mfm4aa
- Posts: 70
- Joined: 13 Feb 2013 14:02
- Location: Europe
#13
Post
by mfm4aa » 23 Mar 2013 08:06
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.
-
foxidrive
- Expert
- Posts: 6031
- Joined: 10 Feb 2012 02:20
#14
Post
by foxidrive » 23 Mar 2013 09:22
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]
-
mfm4aa
- Posts: 70
- Joined: 13 Feb 2013 14:02
- Location: Europe
#15
Post
by mfm4aa » 23 Mar 2013 09:40
Really nice and fast