Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

extract line number from a text file

1,847 views
Skip to first unread message

Abhijat

unread,
Jan 31, 2012, 9:34:57 PM1/31/12
to
Hi Group,
I am wondering if its possible in DOS batch programming to return the
line number of an ascii/text file based on some searchable string. In
R or UNIX, I would have used grep, but I am not sure how to accomplish
the same in DOS.
For example, I have a data file , say file1.txt, where I need to find
the line numbers where "START_OF_DATA" and "END_OF_DATA" are written.

Thanks a lot.

Kind Regards,
Abhijat.

billious

unread,
Jan 31, 2012, 10:31:21 PM1/31/12
to

"Abhijat" <ursab...@gmail.com> wrote in message
news:f1e2f032-9599-4f78...@hb4g2000vbb.googlegroups.com...
@echo off
:: display line numbers in file where strings appear
for /f "tokens=1*delims=[]" %%i in (
' find /v /n "" abhijatn.txt ' ) do if not "%%j"=="" (
for %%m in (%*) do (
echo %%j|find "%%m" >nul
if not errorlevel 1 echo line %%i found %%m
)
)

when fed with a textfile 'abhijatn.txt'

test file - line 2 is empty

findme on line three
nothing on 4
findme again on line 5
sixth and last - nothing to find

Then

thisbatch findme
yields

line 3 found findme
line 5 found findme

thisbatch findme line
yields

line 1 found line
line 3 found findme
line 3 found line
line 5 found findme
line 5 found line




foxidrive

unread,
Feb 1, 2012, 3:33:51 AM2/1/12
to
@echo off
findstr /n /c:"START_OF_DATA" /c:"END_OF_DATA" file.txt


There are ports of grep for Windows too.



--
Mic

Abhijat

unread,
Feb 1, 2012, 5:36:41 AM2/1/12
to
Foxidrive- as always, your code works like a charm.. thanks!
The only questions that I wud have are
1. How to feed this value in a variable, say ABC
2. How to only get the line numbers and not line number: STRING.
Thanks again.

foxidrive

unread,
Feb 1, 2012, 5:49:35 AM2/1/12
to
On 1/02/2012 21:36, Abhijat wrote:
> On Feb 1, 3:33 am, foxidrive <foxidr...@gotcha.woohoo.invalid> wrote:
>> On 1/02/2012 13:34, Abhijat wrote:
>>
>>> Hi Group,
>>> I am wondering if its possible in DOS batch programming to return the
>>> line number of an ascii/text file based on some searchable string. In
>>> R or UNIX, I would have used grep, but I am not sure how to accomplish
>>> the same in DOS.
>>> For example, I have a data file , say file1.txt, where I need to find
>>> the line numbers where "START_OF_DATA" and "END_OF_DATA" are written.
>>
>> @echo off
>> findstr /n /c:"START_OF_DATA" /c:"END_OF_DATA" file.txt
>>
>> There are ports of grep for Windows too.
>>

> 1. How to feed this value in a variable, say ABC
> 2. How to only get the line numbers and not line number: STRING.
> Thanks again.

That's ok. Try this:


@echo off
for /f "tokens=1 delims=:" %%a in ('
findstr /n /c:"START_OF_DATA" file.txt
') do set start=%%a

for /f "tokens=1 delims=:" %%a in ('
findstr /n /c:"END_OF_DATA" file.txt
') do set end=%%a

echo %start%,%end%


--
Mic

Abhijat

unread,
Feb 1, 2012, 6:02:00 AM2/1/12
to
Hi foxidrive,
I am afraid start and end return the findstr command itself. Am I
missing something?
Thanks.

Abhijat

unread,
Feb 1, 2012, 6:19:23 AM2/1/12
to
This is perfect!!
Many thanks.

Timo Salmi

unread,
Feb 2, 2012, 4:58:45 AM2/2/12
to
On 01.02.2012 12:49 foxidrive wrote:
> On 1/02/2012 21:36, Abhijat wrote:
>>>> For example, I have a data file , say file1.txt, where I need to find
>>>> the line numbers where "START_OF_DATA" and "END_OF_DATA" are written.

> @echo off
> for /f "tokens=1 delims=:" %%a in ('
> findstr /n /c:"START_OF_DATA" file.txt
> ') do set start=%%a
>
> for /f "tokens=1 delims=:" %%a in ('
> findstr /n /c:"END_OF_DATA" file.txt
> ') do set end=%%a
>
> echo %start%,%end%

Note one subtle catch, if the datafile contains empty lines. FINDSTR
will count them in (which is more logical), but, as we know, many batch
script operations will skip the empty lines. Thus in some cases, if one
uses the FINDSTR-generated information in the actual production batch,
the results might be out of synch.

All the best, Timo

--
Prof. (emer.) Timo Salmi, Vaasa, Finland
https://twitter.com/TimoSalmi
http://www.netikka.net/tsneti/homepage.php
Useful CMD script tricks http://www.netikka.net/tsneti/info/tscmd.php

foxidrive

unread,
Feb 2, 2012, 5:20:30 AM2/2/12
to
On 2/02/2012 20:58, Timo Salmi wrote:
> On 01.02.2012 12:49 foxidrive wrote:
>> On 1/02/2012 21:36, Abhijat wrote:
>>>>> For example, I have a data file , say file1.txt, where I need to find
>>>>> the line numbers where "START_OF_DATA" and "END_OF_DATA" are written.
>
>> @echo off
>> for /f "tokens=1 delims=:" %%a in ('
>> findstr /n /c:"START_OF_DATA" file.txt
>> ') do set start=%%a
>>
>> for /f "tokens=1 delims=:" %%a in ('
>> findstr /n /c:"END_OF_DATA" file.txt
>> ') do set end=%%a
>>
>> echo %start%,%end%
>
> Note one subtle catch, if the datafile contains empty lines. FINDSTR
> will count them in (which is more logical), but, as we know, many batch
> script operations will skip the empty lines. Thus in some cases, if one
> uses the FINDSTR-generated information in the actual production batch,
> the results might be out of synch.
>
> All the best, Timo

True indeed, Timo.


As we often do, the code answers what was asked for, not what *might* be required.

:)


The perennial query is 'what do you need to do' but people ask the questions including information from 'this is what *I think* I need to know'.



--
Mic

Timo Salmi

unread,
Feb 2, 2012, 6:31:37 AM2/2/12
to
On 02.02.2012 12:20 foxidrive wrote:
>>> for /f "tokens=1 delims=:" %%a in ('
>>> findstr /n /c:"END_OF_DATA" file.txt
>>> ') do set end=%%a

>> script operations will skip the empty lines. Thus in some cases, if one
>> uses the FINDSTR-generated information in the actual production batch,
>> the results might be out of synch.

> As we often do, the code answers what was asked for, not what *might* be required.

Just for the sake of the exercise:

What to do if one for some reason needs to ignore the empty lines. It
gets more complicated, but still is doable. Let's, however, only
consider the END_OF_DATA for brevity.

@echo off & setlocal enableextensions enabledelayedexpansion
set filename_=C:\_D\TEST\My test file.txt
set linecount_=
for /f "usebackq tokens=1 delims=:" %%a in (
`type "%filename_%"`) do (
set /a lineCount=!lineCount!+1
echo "%%a"|find "END_OF_DATA">nul
if !errorlevel! EQU 0 set endDataNR_=!lineCount!)
echo lineCount=%lineCount% endDataNR_=%endDataNR_%
endlocal & goto :EOF

More e.g. in http://www.netikka.net/tsneti/info/tscmd064.htm

foxidrive

unread,
Feb 2, 2012, 6:50:13 AM2/2/12
to
On 2/02/2012 22:31, Timo Salmi wrote:
> On 02.02.2012 12:20 foxidrive wrote:
>>>> for /f "tokens=1 delims=:" %%a in ('
>>>> findstr /n /c:"END_OF_DATA" file.txt
>>>> ') do set end=%%a
>
>>> script operations will skip the empty lines. Thus in some cases, if one
>>> uses the FINDSTR-generated information in the actual production batch,
>>> the results might be out of synch.
>
>> As we often do, the code answers what was asked for, not what *might* be required.
>
> Just for the sake of the exercise:
>
> What to do if one for some reason needs to ignore the empty lines. It
> gets more complicated, but still is doable. Let's, however, only
> consider the END_OF_DATA for brevity.
>

Your code has delims and tokens set to things that change the data, Timo.

This works here, and I used a different method of checking for the string, avoiding the pipe and external program which slows such a test down considerably.


@echo off & setlocal enableextensions enabledelayedexpansion
set filename_=C:\_D\TEST\My test file.txt
set linecount_=
for /f "delims=*" %%a in (
'type "%filename_%"'
) do (
set /a lineCount=!lineCount!+1
set "var=%%a"
set "var=!var:END_OF_DATA=!"
if "!var!" NEQ "%%a" set endDataNR_=!lineCount!
)
echo lineCount=%lineCount% endDataNR_=%endDataNR_%
endlocal & goto :EOF



Cheers
Mic
0 new messages