473,385 Members | 1,185 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Extracting From A Text File

Hi I'm searching for a string occurance in a text file. I find the string ok and write the results to a log file. But on the line above is also some information I need. How can i get that. The string occurs a number of times, but not an exact match for the string above. Can you help please: see code

Expand|Select|Wrap|Line Numbers
  1. #!C:\\Perl\\bin 
  2.  
  3. use File::Copy;
  4. use Archive::Zip qw( :ERROR_CODES :CONSTANTS );
  5.  
  6. ###############################################################
  7. #Get the date,# Months are 0-11!,# Years are 1900 out!
  8. ###############################################################
  9. ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) = gmtime(time);
  10. $mon = $mon+1;
  11. $year = $year+1900;
  12.  
  13. ###############################################################
  14. #set the variables
  15. ###############################################################
  16. $drive = "Y:";
  17. $dir = "backup";
  18. $filename = "Alllogfiles_";
  19. $date = sprintf"%s_%s_%s",$year,$mon,$mday;
  20. $comp_name = "Misys-FromLab"; 
  21. #$comp_name = "Misys-ToLab"; 
  22. $searchstring = "EXCEPTION";
  23. $errorfile = "MisysFromLabErrors$date.txt";
  24. ###############################################################
  25. #Add them together for the archive location
  26. ###############################################################
  27. $fopen = "$drive\\$dir\\$filename$date.zip";
  28.  
  29. ###############################################################
  30. #working directory
  31. ###############################################################
  32. $perltemp = "c:\\perltemp";
  33.  
  34. ###########################
  35. #Get rid of any old files
  36. ###########################
  37. system("del $perltemp\\*.log");
  38. system("del $perltemp\\All*.Zip");
  39.  
  40. ###############################################
  41. #get the archive file
  42. ###############################################
  43. copy ($fopen, $perltemp);
  44.  
  45. ###################################################
  46. #now open the archive
  47. ###################################################
  48. my $opt = x;
  49. my $ofile = "$perltemp\\$filename$date.zip";
  50.  
  51. # extract a zip file
  52. if ($opt=~/x/i) {
  53.     print "Extracting $ofile...\n";
  54.     $zip = Archive::Zip->new();
  55.     die 'Error reading zip file.' if $zip->read( $ofile ) != AZ_OK;
  56.     my @members = $zip->members();
  57.     foreach $element(@members) {
  58.         print "$element\n";
  59.         $zip->extractMember($element);
  60.     }
  61.     print "Done!\n";
  62. }
  63.  
  64. #############################################################
  65. #Search for Exceptions in FromLab
  66. ############################################################
  67. opendir(DIR, "$perltemp") || die "Could not open $dir $!";
  68.  
  69. while($name = readdir(DIR)) {    
  70.     ##Put the names in an array, latest files 1st
  71.     if ($name =~ /$comp_name/) {
  72.         print "Beginning search for Order Exceptions\n";
  73.         print "puting name = $name in the array\n";
  74.         unshift(@files, $name);
  75.     }
  76. }
  77.  
  78. closedir(DIR);
  79.  
  80. open (FHNDL, ">$errorfile") || die "ERROR Could not open $file: $!n";
  81.  
  82. foreach (@files) {
  83.     my $logfile = $_;
  84.     #print "searching  = $logile\n";
  85.  
  86.     open(SEARCHFILE, "<$perltemp/$logfile") || die "Could not open file $matchesboth : $!";
  87.  
  88.     while (<SEARCHFILE>) {
  89.         #print "chomping file $dir, line = $line"; 
  90.         if ($_ =~/$searchstring/){
  91.  
  92.             #print FHNDL "matched string = $1 - file = $logfile - $string = $_\n";
  93.             print FHNDL "matched string in $logfile - $string = $_\n";
  94.             print FHNDL "$###############################################################################################################################\n";
  95.         }
  96.     } 
  97. }
  98. close (FHNDL);
  99. exit;
  100.  
this gives me

matched string in Misys-FromLab-29 Aug 2007 15.log - = 29 Aug 2007 15:00:31.058848 EXCEPTION: System.OverflowException: SqlDbType.SmallDateTime overflow. Value '01/01/1890 00:00:00' is out of range. Must be between 1/1/1900 12:00:00 AM and 6/6/2079 11:59:59 PM.

################################################## ################
just above is anothe line i need
Aug 30 '07 #1
4 3746
KevinADC
4,059 Expert 2GB
You can cache the lines into an array and use the array to get the previous line, this is totally untested but may work as-is, if not it will work after fixing any errors I made:

Expand|Select|Wrap|Line Numbers
  1. my @cached_lines = ();
  2. while (<SEARCHFILE>) {
  3.         #print "chomping file $dir, line = $line"; 
  4.       push @cached_lines,$_; #cache the current line      
  5.         if ($_ =~/$searchstring/){
  6.          if (scalar @cached_lines < 2) {
  7.             print "Not enough cached lines yet to check previous lines\n";
  8.          }
  9.          else {
  10.             my $last_line = $cached_lines[-2];
  11.             #print FHNDL "matched string = $1 - file = $logfile - $string = $_\n";
  12.                print FHNDL "matched string in $logfile - $string = $_\n";
  13.                print FHNDL "previous line in $logfile - $last_line = $_\n";
  14.                print FHNDL "$###############################################################################################################################\n";
  15.            }
  16.        } 
  17.    }
  18. }             
there may be other concerns too like emptying some of the array if it grows too large during processing. If all you ever need is the previous line you could cache the line to a scalar instead of an array and not worry about the array growing too large.
Aug 30 '07 #2
KevinADC
4,059 Expert 2GB
You could also use Tie::File to access the search file instead of open() and a "while" loop. This way the file is treated like a perl array for the most part and you can use a for/foreach loop to loop through the file and access any other line in the file based on the current lines index number.
Aug 30 '07 #3
Hi Kevin, this looks as though it works, thank you very much, I'll look into tie when I get a chance
Aug 31 '07 #4
Kelicula
176 Expert 100+
The Tie::File is wonderful! I tried looping through a filehandle vs. using Tie::File and tie file was MUCH quicker. Also very easy to work with, most all methods (excuse my terminology, javascript throwback) of the array object are supported with tie file.

Good luck!
Sep 2 '07 #5

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

Similar topics

4
by: lecichy | last post by:
Hello Heres the situation: I got a file with lines like: name:second_name:somenumber:otherinfo etc with different values between colons ( just like passwd file) What I want is to extract...
2
by: Avi | last post by:
hi, Can anyone tell me what the problem is and how to solve it The following piece of code resides on an asp page on the server and is used to download files from the server to the machine...
5
by: Michael Hill | last post by:
Hi, folks. I am writing a Javascript program that accepts (x, y) data pairs from a text box and then analyzes that data in various ways. This is my first time using text area boxes; in the past,...
1
by: Cognizance | last post by:
Hi gang, I'm an ASP developer by trade, but I've had to create client side scripts with JavaScript many times in the past. Simple things, like validating form elements and such. Now I've been...
2
by: Kevin K | last post by:
Hi, I'm having a problem with extracting text from a Word document using StreamReader. As I'm developing a web application, I do NOT want the server to make calls to Word. I want to simply...
6
by: Amma | last post by:
Hello Every one , Pls help me to extracting number from a text file since I am new to perl programming . I have a file and need to extract the number after semicolon in that ...
3
by: Clarisa | last post by:
Hello Folks I am working on extracting lines of data in a text file based on the string it contains. This is the text file called info.txt:
6
by: Werner | last post by:
Hi, I try to read (and extract) some "self extracting" zipefiles on a Windows system. The standard module zipefile seems not to be able to handle this. False Is there a wrapper or has...
4
by: poolboi | last post by:
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.