Filter a file

Asked

Viewed 211 times

1

See if anyone can save me, I have a program where every day a TXT is generated as follows:

(16BA=1cc)(83=cliente1@plantao)(80=0)(82=1)(1691=610)(1690(89=cliente@frigorifico)(8A

I need to create another simple program, which removes only the text from this list as follows: nomedocliente@empresa, in the case of this example above, I would have to remove the following data and save to a txt or even put to a MEMO:

client1@Plantao, client@frigorifico

2 answers

1


Here’s an example of how it could be done.

Using TStringList:

var
  _file: TStringList; // StringList para carregar o arquivo
  text, textDest, aux: string;
  posIni, posFim: integer;
begin
  _file := TStringList.Create;
  try
    // carrega o arquivo
    _file.LoadFromFile('NOME_DO_ARQUIVO');
    text := _file.Text;

    // para interagir linha a linha do arquivo
    while Pos('@', text) > 0 do
    begin
      posIni := Pos('=', text);
      posFim := Pos(')', text) - 1;
      aux := Copy(text, posIni+1, posFim-posIni);
      if Pos('@', aux) > 0 then
      begin
        if Trim(textDest) = '' then
          textDest := aux
        else
          textDest := textDest + ',' + aux;
      end;
      Delete(text, 1, posFim + 1);
    end;
    
    if Trim(textDest) <> '' then
    begin
      _file.Clear;
      _file.Text := textDest;
      _file.SaveToFile('NOVO_ARQUIVO_NOVO');
    end
    else
    begin
      ShowMessage('Não existem registros!');
    end;
      
  finally
    _file.Free;
  end;
end;

Using TextFile for better performance

var
  _file: TextFile;
  _fileDest: TStringList;
  text, textDest, aux: string;
  posIni, posFim: integer;
begin
  AssignFile(_file, 'orig.txt');
  Reset(_file);
  try
    while not Eof(_file) do
    begin
      ReadLn(_file, text);
      while Pos('@', text) > 0 do
      begin
        posIni := Pos('=', text);
        posFim := Pos(')', text) - 1;
        aux := Copy(text, posIni+1, posFim-posIni);
        if Pos('@', aux) > 0 then
        begin
          if Trim(textDest) = '' then
            textDest := aux
          else
            textDest := textDest + ',' + aux;
        end;
        Delete(text, 1, posFim + 1);
      end;
    end;

    if Trim(textDest) <> '' then
    begin
      _fileDest := TStringList.Create;
      try
        _fileDest.Text := textDest;
        _fileDest.SaveToFile('dest.txt');
      finally
        _fileDest.Free;
      end;
    end
    else
    begin
      ShowMessage('Não existem registros!');
    end;
  finally
    CloseFile(_file);
  end;
end;
  • only one problem, when the file is large, it hangs, has something that can improve ? The file . TXT has 90KB more has 5 thousand lines...

  • Can you help me in this example if it wouldn’t bother Tiago ? if you were using Readln ? if you can I really appreciate it..

  • Thank you @Tiago Silva.

  • see to help: http://prntscr.com/4t4xax

  • Thanks @Tiago Silva, I’m trying to figure out why it hangs, my file is only 80kb, it’s like I’m giving a LOOP ? small file goes of good.... has some idea friend ?

  • 1

    I think I figured it out here, I’ll test if it works out I’m gonna.... really generating a loop...

Show 1 more comment

1

Contributing to the answer I would use regular expressions to solve your problem, it is simpler and efficient.

A 10,000 dollar purchase was opened and processed in 2 seconds.

See the code.

var
  Texto: string;
  StringList: TStringList;
begin         
    StringList:= TStringList.Create;
    try
      StringList.LoadFromFile( 'c:\aquivo.txt' );

      Texto:= TRegEx.Replace( StringList.Text, '[A-Za-z0-9]+@[A-Za-z0-9]+', '' );

      Memo1.Clear;
      Memo1.Text:= Texto;       
    finally
      FreeAndNil(StringList);
    end;    
  end;

The complete project is in: https://github.com/johnidm/collections-code-kata/tree/master/regular-exprssion-replace

Article talking about regular expressions: http://www.devmedia.com.br/expressoes-regulares-com-delphi-revista-clubedelphi-139/24072

  • unfortunately did not work out the way you put, it does not filter the emails.

  • user7605 emails are in this format "[email protected]", or with ". com" or ". br", because the regular expression I created only filters this format "user@user".

  • That’s right Johni Douglas Marangon, is a list that has several codes inside and not only email understand ? The other answer up there works, more with a list of 5 thousand lines it hangs..... emails is [email protected] format.

  • @user7605 in this case only adapt the regular expression to recognize emails, the ".com.br" will always have or will be optional?

  • I can adapt and regular expression to you

  • Give me an example file with all situations

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.