[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 296
  • Last Modified:

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
0
henryreynolds
Asked:
henryreynolds
1 Solution
 
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now