Reputation:
I am a beginner in Perl and I need your help.
I have a file containing results from processing other files that I would like to analyse. My file contains the following results:
default,primary,secondary,copy_directory,forced_copy_flag,always_allow_dump,type_of_dump,full_memory_dump,
d100spuptl25e0,/dev/lg_dumplv,/dev/sysdumpnull,/var/adm/ras,1,1,fault,disallow,
doc1,/del,/dev/null,/ras,1,0,fw-assisted,disallow,
doc2,/dev/lg_dumplv,/dev/sysdumpnull,//ras,1,1,fw,disallow,
doc3,/dev/lg_dumplv,/dev/sysdumpnull,/var/adm/ras,1,fault,fw-assisted,disallow,
doc4,5,fault,7,8,9,10,disallow,
The first field on each line is the name of the file; the first default
file should be ignored: only the other files must be treated.
I would like the names of the files where one of the values is fault
, as well as a count of such files.
In the present case here the result would be:
number of files: 3
File names are:
d100spuptl25e0
doc3
doc4
Upvotes: 0
Views: 48
Reputation: 126722
It is important that you try to solve the problem yourself, and I really shouldn't be offering this without insisting that you make at least some effort. But I tire of constantly pressing the point and today it is easier just to craft a solution. Just don't expect to get away with it another time!
This will do as you ask
use strict;
use warnings;
use List::Util 'any';
open my $fh, '<', 'myfile.txt' or die $!;
my @files;
while ( <$fh> ) {
next unless /\S/;
chomp;
my ($file, @fields) = split /,/;
next if $file eq 'default';
push @files, $file if any { $_ eq 'fault' } @fields;
}
printf "number of files: %d\n", scalar @files;
print "File names are:\n";
print "$_\n" for @files;
output
number of files: 3
File names are:
d100spuptl25e0
doc3
doc4
Upvotes: 1