# Create some files
$touch {1..3}{a..c}.{ctl,log}
$ls
1a.ctl 1a.log 1b.ctl 1b.log 1c.ctl 1c.log 2a.ctl 2a.log 2b.ctl
+ 2b.log 2c.ctl 2c.log 3a.ctl 3a.log 3b.ctl 3b.log 3c.ctl 3c.
+log junk.pl
$echo "SRS_222" >> 1c.ctl
$echo "SRS_333" >> 1c.log
$echo 'SRS_111SRS_2222SRS_3333' > 1a.log
#!/usr/bin/env perl
use strict;
use warnings;
use Data::Dumper;
opendir(my $dh, '.') or die $!;
my @files = map { /(.*)\.ctl$/ }readdir $dh;
my %srss;
for (@files) { # $_ is set to each element in @files
for my $extension(qw(ctl log)) {
my $file = "$_.$extension";
open (my $fh, $file) or die $!;
while (my $line = <$fh>) {
while ($line =~ /SRS[_\s]?(\d+)/g) {
$srss{$1} = 1;
}
}
}
}
print join '|', sort {$a <=> $b} keys %srss;
# 111|222|333|2222|3333
But maybe later you want to store more information such as what file the line was found in. A more complex data structure is needed.
#!/usr/bin/env perl
use strict;
use warnings;
use Data::Dump;
opendir(my $dh, '.') or die $!;
my @files = map { /(.*)\.ctl$/ }readdir $dh;
my %hashSlice;
@hashSlice{@files} = undef;
my $srss;
$srss->{files} = { %hashSlice } ;
for (@files) { # $_ is set to each element in @files
for my $extension(qw(ctl log)) {
my $file = "$_.$extension";
open (my $fh, $file) or die $!;
while (my $line = <$fh>) {
while($line =~ /SRS[_\s]?(\d+)/g) {
$srss->{files}{$_}{$extension}{$1} = '
+Found';
}
}
}
}
dd $srss;
{
files => {
"1a" => { log => { 111 => 1, 2222 => 1, 3333 => 1 } },
"1b" => undef,
"1c" => { ctl => { 222 => 1 }, log => { 333 => 1 } },
"2a" => undef,
"2b" => undef,
"2c" => undef,
"3a" => undef,
"3b" => undef,
"3c" => undef,
},
}
While more complicated this has the advantage of showing which files did not have matches, which ones did, and what file extension they were found in. You can see all of the perl documentation with the perldoc commands. 'perldoc perl' will give you tons of material to read on the command line. There is a ton of references out there to learn from. There is a really concise tutorial at Learn Perl in 2.5 hours. Hope this helps. |