Page 1 of 1

Specify which line to read in "Findstr"

Posted: 06 Jan 2012 22:13
by Rileyh
I have a "for /f" string:

Code: Select all

@echo off
set "$file=test.txt"
for /f "tokens=1*" %%a in ('findstr /v /i "printline" "%$file%"') do (
set "command=%%a"
set "value=%%b"
setlocal enabledelayedexpansion
echo !value!
endlocal
goto :break
)

:break
(file continues)


In the "findstr" part, it finds all occurrances of "printline" in "%$file%". I need it to search only line "x" (the line it needs to search will vary)
How can I do that?

Regards,
Rileyh

Re: Specify which line to read in "Findstr"

Posted: 07 Jan 2012 08:11
by dbenham
Your code actually finds all lines that do NOT contain "printline" because of the /v option. I'm going to assume you actually want the line(s) with "printliine", so I will remove the /v option. Add it back if needed.

The solution is to prefix each line that is found with the line number using the FINDSTR /n option. You then simply adjust your FOR /F options to compensate and add an IF to conditionally process the matching line only if it is line number %x%. If the command can contain a colon, then the solution will be a bit more complicated because FOR /F options will not allow you to parse properly because the line number prefix also includes a colon.

Assuming your command cannot contain a colon, then this should work (untested). I'm arbitrarily setting x to 50 in this code. I also undefine command and value so that they are guaranteed to be undefined if the matching line is not found.

Code: Select all

@echo off
set "$file=test.txt"
set "x=50"
set "command="
set "value="
for /f "tokens=1,2* delims=: " %%a in ('findstr /n /i "printline"') do if "%%a"=="%x%" (
  set "command=%%b"
  set "value=%%c"
  setlocal enabledelayedexpansion
  echo !value!
  endlocal
  goto :break
)
:break
(file continues)


An alternative way, perhaps more efficient, would be to use a 2nd FINDSTR to find the correct line instead of an IF condition. This solution does not require the GOTO.

Code: Select all

@echo off
set "$file=test.txt"
set "x=50"
set "command="
set "value="
for /f "tokens=1,2* delims=: " %%a in ('findstr /n /i "printline" ^| findstr /b "%x%:"') do (
  set "command=%%b"
  set "value=%%c"
  setlocal enabledelayedexpansion
  echo !value!
  endlocal
)
(file continues)


I'm wondering if your command is "printline" followed by a space. If so, then a more precise solution would look for "printline " at the beginning of each line:

Code: Select all

@echo off
set "$file=test.txt"
set "x=50"
set "command="
set "value="
for /f "tokens=1,2* delims=: " %%a in ('findstr /n /i /b "printline " ^| findstr /b "%x%:"') do (
  set "command=%%b"
  set "value=%%c"
  setlocal enabledelayedexpansion
  echo !value!
  endlocal
)
(file continues)


Dave Benham

Re: Specify which line to read in "Findstr"

Posted: 08 Jan 2012 04:08
by Rileyh
Unfortuantely none of them worked. The console was blank and I could type stuff but nothing happened.
I am currently testing, but I don't know why (so far).

Regards,
Rileyh

Re: Specify which line to read in "Findstr"

Posted: 08 Jan 2012 11:57
by Squashman
It might actually help if you could post an example of your input and what you expect the output to be.

Re: Specify which line to read in "Findstr"

Posted: 12 Jan 2012 00:33
by Rileyh
Example input:
(In test.txt)

Code: Select all

(something random)
printline Hello World
(something random)


I want it to look on a line number previously specified, so for the sake of this scenario I will make the line number be line 1 and check for "printline" on it (in this case the first line does not have printline on it). So it will continue with the batch file. At the end of the batch file will be this:

Code: Select all

set "count=%count% + 1"
set /a count=%count
goto :loop

":loop" being at the top of the batch file, where this code should be.
The "count" is the line number it should search on.

SCENARIO:
(In test.txt)

Code: Select all

(empty line)
printline Hello
(empty line)


It should search on line 1, find nothing, goto the end of the batch file, set "%count%" +1 and goto the top, where it will search on line 2, echo "hello" and goto the end, loop, read line three, find nothing and then exit.

See what I mean?

Thank you in advance,
Rileyh