I have several output files (*.out) contain a lot of information. I would like to have a program can just sort specific data from each output file and save them to the same filename with txt extension file.

While reading each line (from *.out) if you are on a line that says "state summary:" then save next 10 lines (into *.txt). Please advise. Thanks!

program sort;
uses
dos;

var
k,l,loc : integer;
fx : array[1..1000] of string;
Dirinfo : SearchRec;
inf : Text;
ouf1, ouf2 : Text;
str : string;

begin
k := 0;
FindFirst('*.out', Archive, Dirinfo);
while DosError = 0 do
begin
k := k + 1;
loc := pos('.',Dirinfo.name);
fx[k] := Dirinfo.name;
FindNext(Dirinfo);
end;

for l:= 1 to k do
begin
assign(inf,fx[l]); reset(inf);
delete(fx[l],loc,length(Dirinfo.name));
assign(ouf1,fx[l]+'_SRV.txt'); rewrite(ouf1);
assign(ouf2,fx[l]+'_STR.txt'); rewrite(ouf2);

while NOT eof(inf) do begin
readln(inf,str) ;
if Pos('SERVICE LIMIT STATE SUMMARY:',str) > 0 then
while Pos('Notes:',str) = 0 do begin
readln(inf,str);
writeln(ouf1,str);
end

else if Pos('STRENGTH LIMIT STATE SUMMARY:',str) > 0 then
while Pos('Notes:',str) = 0 do begin
readln(inf,str);
writeln(ouf2,str);
end
else
end;
close(inf); close(ouf1); close(ouf2);
end;
end.

var
  inFile, outFile: TStringList;
  Row, rowCount: integer;
begin
  inFile := TStringList.Create;
  try
    Row := 0;
    inFile.LoadFromFile('inFilename.out');
    while Row < inFile.Count do
    begin
      if Pos('state summary:',inFile.Strings[Row]) > 0 then
      begin
         inc(Row);
         outFile := TStringList.Create;
         try
           rowCount := 0;
           while (rowCount < 10) do
           begin
             outFile.Add(inFile.Strings[Row]);
             inc(Row);
             inc(rowCount);
             if Row = inFile.Count then Break;
           end;
           outFile.SaveToFile('outFilename.txt');
         finally
           outFile.Free;
         end;
      end else
          inc(Row);
    end;
  finally
    inFile.Free;
  end;
var
  inFile, outFile: TextFile;
  curLine: string;
  rowCount: integer;
begin
  AssignFile(inFile,'inFilename.out');
  Reset(inFile);
  AssignFile(outFile,'outFilename.txt');
  Reset(outFile);
  try
    while not Eof(inFile) do
    begin
      ReadLn(inFile,curLine);
      if Pos('state summary:', curLine) > 0 then
      begin
         rowCount := 0;
         repeat
           if Eof(inFile) then Break;
           ReadLn(inFile, curLine);
           WriteLn(outFile,curLine);
           inc(rowCount);
         until rowCount = 10;
      end;
    end;

  finally
    CloseFile(inFile);
    CloseFile(outFile)
  end;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.