By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,848 Members | 1,290 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,848 IT Pros & Developers. It's quick & easy.

Problem extracting data from text file

100+
P: 170
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

Expand|Select|Wrap|Line Numbers
  1. my $service_line = index ($original, "T11");
  2. my $service = substr ($original, ($service_line), 3);   
  3.  
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?
May 12 '08 #1
Share this Question
Share on Google+
4 Replies


numberwhun
Expert Mod 2.5K+
P: 3,503
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

Expand|Select|Wrap|Line Numbers
  1. my $service_line = index ($original, "T11");
  2. my $service = substr ($original, ($service_line), 3);   
  3.  
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:

Expand|Select|Wrap|Line Numbers
  1. open(FILE, "<file.txt");
  2. my @array;
  3.  
  4. while(<FILE>)
  5. {
  6.     my $text;
  7.     my $date;
  8.     my $time;
  9.  
  10.     ($text, $date, $time) = split (/\s/, $_);
  11.     push(@array, $date);
  12. }
  13.  
That should do the trick, but please know its untested.

Regards,

Jeff
May 12 '08 #2

Ganon11
Expert 2.5K+
P: 3,652
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:

Expand|Select|Wrap|Line Numbers
  1. my @date_list = ();
  2. # Open the file for reading:
  3. open my $filehandle, '<', 'file_name-txt'; # You could add 'or die "Error!  Couldn't open file: $!" after this to error check.
  4. while (<$filehandle>) { # While the file still has contents, read these into $_
  5.    $_ =~ /Date (\d{4}-\d{2}-\d{2})/;
  6.    my $date = $1;
  7.    push @date_list, $date;
  8. }
  9. # Now @date_list holds all the dates, in order of appearance in the text file.
May 12 '08 #3

KevinADC
Expert 2.5K+
P: 4,059
if the lines are fixed length the way to go is unpack(), if not, then regexp, either using split or not.
May 12 '08 #4

100+
P: 170
that's really a lot of suggestion
in the end i did this this

Expand|Select|Wrap|Line Numbers
  1. my ($data, $data1);
  2. open (FILE1,"file.txt") || die "Couldn't OPEN file!!!\n";      
  3. @$data = <FILE1>;                                              
  4. close(FILE1) || die "Couldn't CLOSE file properly!!!\n";    
  5.  
  6. foreach $data1 (@$data)
  7. {
  8. chomp ($data1); 
  9. if ($data1 =~ /DATE/){
  10. my $service_line = index ($data1, "DATE");
  11. my $service = substr ($data1, ($service_line+5),10);      
  12. print ("$service\n");     
  13. }}
  14.  
it did the job too..is my code alright?
May 12 '08 #5

Post your reply

Sign in to post your reply or Sign up for a free account.