Computing.Net > Forums > Programming > DOS BAT - Read lines from bottom up

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

DOS BAT - Read lines from bottom up

Reply to Message Icon

Name: bismarkcount
Date: May 31, 2007 at 14:25:09 Pacific
OS: Windows xp
CPU/Ram: inter core2 duo 1.6ghz 1g
Product: Dell
Comment:

I need help to know how can i read a text file from bottom up, and save each line in different variables until i find a line in the file starting with a certain character (-).

i am using some "read last x lines" code, which is very useful but i just cant figure out how to stop reading when i find the line starting with the specific character

thnx a lot



Sponsored Link
Ads by Google

Response Number 1
Name: Razor2.3
Date: May 31, 2007 at 19:55:38 Pacific
Reply:

Why is everyone so interested in CMD command scripts? I just don't get it.

To answer your question, I think something like this would work:
for /f "tokens=*" %%a in ('findstr "^-" myFile.txt') do set line=%%a


0

Response Number 2
Name: Mechanix2Go
Date: May 31, 2007 at 20:36:34 Pacific
Reply:

Try this.

[Works on files with less than 100 lines.]

::== r5.bat
@echo off
setLocal EnableDelayedExpansion

call :clear

for /f "tokens=1,* delims=[]" %%a in ('find /n /v "" ^< myfile') do (
if %%a LSS 10 (set N=0%%a) else (set N=%%a)
echo !N! %%b >> myfile.num
)
sort /r < myfile.num > myfile.rev
for /f "tokens=1,* delims= " %%a in (myfile.rev) do (
echo %%b | findstr /b /c:"-"
)

:clear
for %%f in (.num .rev .new) do if exist myfile%%f del myfile%%f
::==


=====================================
If at first you don't succeed, you're about average.

M2



0

Response Number 3
Name: Mechanix2Go
Date: May 31, 2007 at 20:54:45 Pacific
Reply:

Hi Razor2.3

"Why is everyone so interested in CMD command scripts? I just don't get it."

It must be the thrill of victory and the agony of defeat thing.


=====================================
If at first you don't succeed, you're about average.

M2



0

Response Number 4
Name: FishMonger
Date: May 31, 2007 at 22:32:56 Pacific
Reply:

"It must be the thrill of victory and the agony of defeat thing."

lol, I like that, but the truth is probably more like:

The thrill of agony and the agony of why didn't I do I write the first time.


0

Response Number 5
Name: Razor2.3
Date: May 31, 2007 at 23:08:42 Pacific
Reply:

Fish, I'm not sure if that's intentional or not, but it's funny either way.


0

Related Posts

See More



Response Number 6
Name: FishMonger
Date: June 1, 2007 at 00:45:09 Pacific
Reply:

Ya, It was meant to be a joke...I keep forgetting to put in the smilies.

I'm sure that the OP has other requirements, but here's 2 (1 liner) perl solutions.

===============================================================================

C:\>perl -MTie::File -e "tie @f, 'Tie::File', shift; for(reverse @f){print $_ and last if /^\s*-/ }" file.txt

C:\>perl -MFile::ReadBackwards -e "tie *F, 'File::ReadBackwards', shift; while(<F>){print and last if /^\s*-/}" file.txt


0

Response Number 7
Name: FishMonger
Date: June 1, 2007 at 09:09:29 Pacific
Reply:

For completeness, I guess I should show a shorter command, but if you're processing a large file, this is less efficient because it uses more resources.

====================================================

C:\test>perl -e "@f = reverse <>; for(@f){print and last if /^\s*-/ }" file.txt

Note: none of these Perl commands are limited to a 100 line file like M2's batch script.


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: DOS BAT - Read lines from bottom up

reading lines from text file (java) www.computing.net/answers/programming/reading-lines-from-text-file-java/12276.html

Read lines from .txt file in DOS? www.computing.net/answers/programming/read-lines-from-txt-file-in-dos/15219.html

Read last line from newest file in folder www.computing.net/answers/programming/read-last-line-from-newest-file-in-folder/19280.html