Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can i read a text file line by line and test all lines for strings

Status
Not open for further replies.

moreth

Programmer
Dec 3, 2001
3
BR
I have a .log file and i want to get only the lines that have the word "commands" on it.

Thanks Rodrigo Moreth
 
Read the lines in a string or string list and use the pos function

ex:

i := pos ('commands', TextLineString)

if commands exists, it will store the index where it is in TextlineString.

No occurence returns 0

Check also out FAQ218-360 about string handling in pascal (and delphi)

Regards
S. van Els
SAvanEls@cq-link.sr
 
This would work ...

Procedure TForm1.Proc_Command (var Textline :string);
var File : Textfile;
index : integer;
Textline : string;
begin
AssignFile(File,'C:\MyLog.log');
Reset(File);
While not Eof do
begin
Readln (File,Textline);
index := pos('commands', Textline);
if index <> 0 then Memo1.lines.append(Textline);
end;
Close(File);
end;

I'll explain the lines each..
AssignFile([var:Textfile],[LocationFile:String]); This assigns the file to a variable (TextFile);

Reset([var:Textfile]); This lets you begin READING the file, u have to use Rewrite to be able to Writeln the file

While not eof do .. this means While NOT EndOfFile Do

Readln([var:Textfile],[TextLine:String]); just a normal Readln, only difference is that the first paramater is used to point out the file. (baaad english :))

index := pos(Stringyoulookfor:String,Textline:String); if commands exists, it will store the index where it is in TextlineString.
No occurence returns 0 (courtesy of CTRL-C and svanels :))

if index <> 0 then Memo1.lines.append(Textline); if index is not 0 (thus 'commands' exist in the textline) then append the Textline to Memo1.

Close([File:TextFile]); Closes the File.

maybe i have given a little bit too much information but this is my first time too :) E-mail: Rob@matas.nl (till Jan 10, from then on it's Deathwish@winning.com)
 
DOH!.. no edit command, i made a typo

Procedure Proc_Command (var TextLine:String);
should be :
Procedure Proc_Command;

the var came of my own program :)
sorry..
grtz,
RobPouwelse E-mail: Rob@matas.nl (till Jan 10, from then on it's Deathwish@winning.com)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top