Problem extracting data from text file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • poolboi
    New Member
    • Jan 2008
    • 170

    Problem extracting data from text file

    hi guys

    i've having some problem extracting data from a text file
    example if i got a text file with infos like:

    Date 2008-05-01 Time 22-10
    Date 2008-05-01 Time 21-00
    Date 2008-05-02 Time 19-15
    Date 2008-05-06 Time 18-00


    The Date and Time is like one block
    but i need to extract say the all the dates
    usually i used

    [CODE=perl]
    my $service_line = index ($original, "T11");
    my $service = substr ($original, ($service_line) , 3);
    [/CODE]

    but the problem is this index points to Date only once
    it doesn't go through all the dates...
    any idea how i can achieve getting all the dates extracted in the textfile?
  • numberwhun
    Recognized Expert Moderator Specialist
    • May 2007
    • 3467

    #2
    Originally posted by poolboi
    hi guys

    i've having some problem extracting data from a text file
    example if i got a text file with infos like:

    Date 2008-05-01 Time 22-10
    Date 2008-05-01 Time 21-00
    Date 2008-05-02 Time 19-15
    Date 2008-05-06 Time 18-00


    The Date and Time is like one block
    but i need to extract say the all the dates
    usually i used

    [CODE=perl]
    my $service_line = index ($original, "T11");
    my $service = substr ($original, ($service_line) , 3);
    [/CODE]

    but the problem is this index points to Date only once
    it doesn't go through all the dates...
    any idea how i can achieve getting all the dates extracted in the textfile?
    Personally, I would open the text file for reading, getting a file handle assigned, then cycle through each line, pulling out the date times. You can either put them out to a file, put them into an array, etc. The choice is yours. You could use something like the following:

    [code=perl]
    open(FILE, "<file.txt" );
    my @array;

    while(<FILE>)
    {
    my $text;
    my $date;
    my $time;

    ($text, $date, $time) = split (/\s/, $_);
    push(@array, $date);
    }
    [/code]

    That should do the trick, but please know its untested.

    Regards,

    Jeff

    Comment

    • Ganon11
      Recognized Expert Specialist
      • Oct 2006
      • 3651

      #3
      Your index and substr combination is a tried and true method...in C, C++, or Java. Perl has regular expressions, and these are indeed the powerhouse behind Perl (and why I like it so much right now). Here's what I'd do:

      [CODE=perl]my @date_list = ();
      # Open the file for reading:
      open my $filehandle, '<', 'file_name-txt'; # You could add 'or die "Error! Couldn't open file: $!" after this to error check.
      while (<$filehandle> ) { # While the file still has contents, read these into $_
      $_ =~ /Date (\d{4}-\d{2}-\d{2})/;
      my $date = $1;
      push @date_list, $date;
      }
      # Now @date_list holds all the dates, in order of appearance in the text file.[/CODE]

      Comment

      • KevinADC
        Recognized Expert Specialist
        • Jan 2007
        • 4092

        #4
        if the lines are fixed length the way to go is unpack(), if not, then regexp, either using split or not.

        Comment

        • poolboi
          New Member
          • Jan 2008
          • 170

          #5
          that's really a lot of suggestion
          in the end i did this this

          [CODE=perl]
          my ($data, $data1);
          open (FILE1,"file.tx t") || die "Couldn't OPEN file!!!\n";
          @$data = <FILE1>;
          close(FILE1) || die "Couldn't CLOSE file properly!!!\n";

          foreach $data1 (@$data)
          {
          chomp ($data1);
          if ($data1 =~ /DATE/){
          my $service_line = index ($data1, "DATE");
          my $service = substr ($data1, ($service_line+ 5),10);
          print ("$service\n ");
          }}
          [/CODE]

          it did the job too..is my code alright?

          Comment

          Working...