Import Text file - not a fixed layout file

Good Day

I need some advice on which way I must go to import an text file. The file is not in a fixed format I have included an sample of the file.

I require the output to be written to a file in the following format.

Sample output.

Heading1,Sub Heading 1,57,7257.00,73.44,7,183.56,CR
Heading1,Sub Heading 2,889,1083.49,104740.44,103656.95,DB
Heading1,Sub Heading 3,1,8340.49,104825.88,96,485.39,DB
Heading1,Total,947,8340.49,104825.88,96485.39,DB

Heading2,Sub Heading 1,0,80.71,1.50,79.21,CR
Heading2,Sub Heading 2,0,645.51,404.92,240.59,CR
Heading2,Sub Heading 3,0,0,0,CR
Heading2,Total,0,726.22,406.42,319.80,CR

the same with Heading 3...

I need to take each heading  and then read the sub details and the sub details have the following columns.
Count,Credit,Debit,Total.

Any sample would be appreciated.

Thank you very very much

Sample.txt
henryreynoldsAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mlmccCommented:
WHat language are you going to use?

mlmcc
0
henryreynoldsAuthor Commented:
I would prefer delphi, but if not possible I could try perl.

I did work on some perl scripts, I know only the basics, but Delphi is my first choice.

thanx
0
Ferruccio AccalaiSenior developer, analyst and customer assistance Commented:
It's a ltl bit difficult for at least two reasons:

1st, there are not fixed positions for the data

2nd, for the REC column sometimes there's a value and sometimes (for 0) there's not, so it's quite impossible to know where to find some data if they are not definitely there
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

aikimarkCommented:
@henryreynolds,

1. Does this sample.txt file accurately represent the input file?  It is very unusual to see columns that aren't aligned.

2. Did you mean REC when you wrote Count?

3. Did you mean to include a blank line in your output example?
0
Ferruccio AccalaiSenior developer, analyst and customer assistance Commented:
Try this, just have to modify the SUb Header Index but it's quite what you're asking for
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
  Function TrimToOne(Value: String): String;
  var
    s, s1: String;
  begin
    s := Trim(Value);
    repeat
      s1 := s;
      s := StringReplace(s, '  ', ' ', [rfReplaceAll]);
    until s = s1;
    Result := s;
  end;

var
  List, TmpList, OutputList: TStrings;
  Input, Output, HIndex, Header: String;
  i, x, p: Integer;
  GetSubDetail: Boolean;
begin
  List := TSTringList.Create;
  TmpList := TSTringList.Create;
  OutputList := TSTringList.Create;
  try
    List.LoadFromFile('c:\sample.txt');
    for i := 0 to List.Count - 1 do
    begin
      Input := List[i];
      if GetSubDetail then
      begin
        if Trim(Input) <> '' then
        begin
          if Pos('TOTAL', Input) > 0 then
          begin
            // get total
            GetSubDetail := False;
            Output := Output+Format('%s,Total', [Header]);
            Input := StringReplace(Input, 'TOTAL', '', []);
            Input := TrimToOne(Input);
            TmpList.delimiter := ' ';
            TmpList.DelimitedText := Input;
            if TmpList.Count < 4 then
              Output := Output+',0'
            else
              Output := Output+Format(',%s', [TmpList[0]]);
            for x := 1 to TmpList.Count-1 do
              Output := Output+Format(',%s', [StringReplace(TmpList[x], ',', '', [])]);
            if (Copy(Output, Length(Output)-1, 1)[1]) in ['A'..'Z'] then
              Insert(',', Output, Length(Output)-1);
            OutputList.Add(Output);
            OutputList.Add('');
            Output := '';
          end
          else
          begin
            p := Pos('Sub Heading ', Input);
            if p > 0 then
            begin
              // get sub detail
              Output := Output+Format('%s,Sub Heading %s', [Header, HIndex]);
              Input := StringReplace(Input, Format('Sub Heading %s', [HIndex]), '', []);
              Input := TrimToOne(Input);
              TmpList.delimiter := ' ';
              TmpList.DelimitedText := Input;
              if TmpList.Count < 4 then
                Output := Output+',0'
              else
              begin
                Output := Output+Format(',%s', [TmpList[0]]);
                TmpList.Delete(0);
              end;
              for x := 0 to TmpList.Count-1 do
                Output := Output+Format(',%s', [StringReplace(TmpList[x], ',', '', [])]);
              if (Copy(Output, Length(Output)-1, 1)[1]) in ['A'..'Z'] then
                Insert(',', Output, Length(Output)-1);
              OutputList.Add(Output);
              Output := '';
            end;
          end;
        end;
      end
      else
      begin
        If Pos('Heading', Input) > 0 then
        begin
          GetSubDetail := True;
          HIndex := Copy(Input, 8, Length(Input)-7);
          Header := Input;
        end;
      end;
    end;
  finally
    showmessage(OutputList.Text);
    OutputList.SaveToFile('C:\OutputSample.txt');
    List.Free;
    TmpList.Free;
    OutputList.Free;
  end;
end;

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
henryreynoldsAuthor Commented:
Hi Experts

I left the office now I will give an update later. thanx for all the help so far

The sample is not 100% correct the columns does align up correctly, I will add a new sample in the morning
0
henryreynoldsAuthor Commented:
Hi Ferruccio68

I dont know what to say, it is AMAZING I just want to review everything then I will give you more feedback.

Thank you very very much
0
henryreynoldsAuthor Commented:
Ferruccio68 thank you very very much you are a star.
0
Ferruccio AccalaiSenior developer, analyst and customer assistance Commented:
Glad to have helped you :)
0
henryreynoldsAuthor Commented:
You have helped me incredibly
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.