
rGeoffrey
User
Oct 27, 2001, 2:04 PM
Post #2 of 2
(4668 views)
|
The car line was not working because in the script you had "car" while in the data file you had "Car". So I added the case insensitive 'i' flag to the regular expressions. To print the extra special lines we keep reading and printing until we find the next line. I am assuming that the next line will always be "Expiry=", so you might need to change that regex or the data to make it work. Here is some code that works...
#!/usr/local/bin/perl use strict; my $dirname = '.'; my $search = 'mary'; opendir (DIR, $dirname) or die "could not open dir, $!\n"; my @files = sort map {"$dirname/$_" } grep { /$search/ } readdir DIR; closedir DIR; foreach my $fileName (@files) { open( NAMES, $fileName ) or die "can't open $fileName, $!"; print "------------------\n"; while (<NAMES>) { if (/Name=/i) { print $_; } elsif (/Location=/i) { print $_; } elsif (/Type of car=/i) { print $_; } elsif (/Special=/i) { print $_; while (<NAMES>) { last if (/Expiry=/i); print $_; } } } close NAMES; } print "------------------\n", "\n\nNumber of matches: ", scalar (@files), " \n\n"; PS. To make code samples look right on these pages it is best to put them in blocks. I have added the spaces inside the braces so it will appear here, but you will want to use them without the spaces.
|