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

DOS BAT - Read lines from bottom up

Reply to Message Icon

Original Message
Name: bismarkcount
Date: May 31, 2007 at 14:25:09 Pacific
Subject: DOS BAT - Read lines from bottom up
OS: Windows xp
CPU/Ram: inter core2 duo 1.6ghz 1g
Model/Manufacturer: 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


Report Offensive Message For Removal


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

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


Report Offensive Follow Up For Removal

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

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



Report Offensive Follow Up For Removal

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

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



Report Offensive Follow Up For Removal

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

"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.


Report Offensive Follow Up For Removal

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

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


Report Offensive Follow Up For Removal


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

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


Report Offensive Follow Up For Removal

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

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.


Report Offensive Follow Up For Removal






Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: DOS BAT - Read lines from bottom up

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software




Have you ever used OpenOffice?

Yes, as my main suite.
Yes, occationally.
Yes, but only once.
No, never.


View Results

Poll Finishes In 5 Days.
Discuss in The Lounge