Reading a specific line from a text document

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
lordlaxton
Posts: 1
Joined: 09 Mar 2016 02:09

Reading a specific line from a text document

#1 Post by lordlaxton » 09 Mar 2016 02:17

I have a text file that reads something like this (it is located in the same folder as the Bat file).

13
7
46

In my program i want to take a specific line (eg. line 2: 7) and set it as a variable.
I am using this section of code to get the last line in the file but i want to be able to get a specific line how can i modify this code or write another code to do this?

for /f "delims=" %%a in (test.txt) do (

set text=%%a

)

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Reading a specific line from a text document

#2 Post by foxidrive » 09 Mar 2016 03:22

This uses a native Windows batch script called Findrepl.bat written by Aacini, which uses jscript to make it robust and swift.
viewtopic.php?f=3&t=4697

Place Findrepl.bat in the same folder as the batch file, or in a folder that is on the system path.

There is also copy on Dropbox (unblock it after downloading):
https://www.dropbox.com/s/rfdldmcb6vwi9xc/findrepl.bat


Code: Select all

@echo off
for /f "delims=" %%a in ('findrepl /o:2:2 ^<test.txt') do set "variable=%%a"
set variable
pause

sambul35
Posts: 192
Joined: 18 Jan 2012 10:13

Re: Reading a specific line from a text document

#3 Post by sambul35 » 09 Mar 2016 08:48

lordlaxton wrote:In my program i want to take a specific line (eg. line 2: 7) and set it as a variable.


Since FOR /F command evaluates each line's tokens in a text file iteratively line by line from top to bottom, this might work for you:

Code: Select all

@echo off
setlocal EnableDelayedExpansion
set count=1
for /f "tokens=*" %%a in (test.txt) do (
if !count! equ 2 (set "text=%%a" & goto :next)
set /a count+=1
)

:next
echo %text%
Last edited by sambul35 on 09 Mar 2016 20:21, edited 1 time in total.

Jer
Posts: 177
Joined: 23 Nov 2014 17:13
Location: California USA

Re: Reading a specific line from a text document

#4 Post by Jer » 09 Mar 2016 19:14

If you always look for a line beyond line #1, here is an alternative:

Code: Select all

@echo off

Set "targetLine=2"
Set "file=test.txt"

set /A "skipTo=%targetLine%-1"
for /f "skip=%skipTo% tokens=*" %%a In (%file%) do set "str=%%a" & GoTo:next
:next
echo found on line %targetLine%: %str%


I use code similar to this to pull a single line from a list, and since I create the list, I add a dummy
line at the top of the text file, then skipping 1 in the for loop would get the first item. In my code
the subtraction is not done.

ShadowThief
Expert
Posts: 1160
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: Reading a specific line from a text document

#5 Post by ShadowThief » 09 Mar 2016 19:29

If you want to avoid using a goto to messily break out of a for loop, you can also simply redirect the file.

Code: Select all

@echo off

set lines_to_skip=2
(
   if %lines_to_skip% gtr 0 for /L %%A in (1,1,%lines_to_skip%) do set /p =
   set /p str=
)<test.txt

echo %str%


This will skip the first two lines and set %str% to the value of the third line.

sambul35
Posts: 192
Joined: 18 Jan 2012 10:13

Re: Reading a specific line from a text document

#6 Post by sambul35 » 10 Mar 2016 06:24

ShadowThief wrote:If you want to avoid using a goto to messily break out of a for loop, you can also simply redirect the file.


I'm curious why do you consider using GOTO from a loop be "messily breakout"? Aren't redirection techniques broadly used in programming since forever? :D

Jer
Posts: 177
Joined: 23 Nov 2014 17:13
Location: California USA

Re: Reading a specific line from a text document

#7 Post by Jer » 10 Mar 2016 10:17

On the subject of goto, why not use it to avoid putting potentially many lines in an 'if' block?
if [condition_is_met] goto:conditionMet
do some stuff
do some more stuff...etc.
:conditionMet
continue on

vs.
if [condition_not_met] (
do all the stuff inside the if-block
)

sambul35
Posts: 192
Joined: 18 Jan 2012 10:13

Re: Reading a specific line from a text document

#8 Post by sambul35 » 10 Mar 2016 10:28

I guess because FOR loop will still unduly continue without infamous GOTO. :cry:

b0gus
Posts: 7
Joined: 02 Mar 2016 12:58

Re: Reading a specific line from a text document

#9 Post by b0gus » 13 Mar 2016 06:21

lordlaxton wrote:In my program i want to take a specific line (eg. line 2: 7) and set it as a variable.
may be so?

Code: Select all

@echo off
set /a NTargetLine=2, N=NTargetLine-1
<test.txt (for /f %%v in ('more +%N%') do set "line=%%v" & goto done)
:done
echo.line number %NTargetLine% in test.txt is:%line%
pause
exit

Post Reply