I have a file reflog with the content as below. There will be items with same name but different extensions. I want to check that for each of the items (file1, file2 & file3 here as example), it needs to be exist in both extensions (.abc and .def). If both extensions exist, it will perform some regex and print out. Else it will just report out with the file name together with extension (ie, if only on of file1.abc or file1.def exists, it will be printed out).
reflog:
file1.abc
file2.abc
file2.def
file3.abc
file3.def
file4.abc
file5.abc
file5.def
file6.def
file8abc.def
file7.abc
file1.def
file9abc.def
file10def.abc
My script is as below (editted from yb007 script), but I have some issues with the output that I don;t know how to resolve. I notice the output is going to be wrong when the reflog file having any file with the name *abc.def (such as ie. file8abc.def & file9abc.def). It will be trim down the last 4 suffix and return the wrong .ext (which is .abc here but I suppose it should be .def).
#! /usr/bin/perl
use strict;
use warnings;
my @files_abc ;
my @files_def ;
my $line;
open(FILE1, 'reflog') || die ("Could not open reflog") ;
open (FILE2, '>log') || die ("Could not open log") ;
while ($line = <FILE1>) {
if($line=~ /(.*).abc/) {
push(@files_abc,$1);
} elsif ($line=~ /(.*).def/) {
push(@files_def,$1); }
}
close(FILE1);
my %first = map { $_ => 1 } @files_def ;
my @same = grep { $first{$_} } @files_abc ;
my @abc_only = grep { !$first{$_} } @files_abc ;
foreach my $abc (sort @abc_only) {
$abc .= ".abc";
}
my %second = map {$_=>1} @files_abc;
my @same2 = grep { $second{$_} } @files_def; #@same and same2 are equal.
my @def_only = grep { !$second{$_} } @files_def;
foreach my $def (sort @def_only) {
$def .= ".def";
}
my @combine_all = sort (@same, @abc_only, @def_only);
print "\nCombine all:-\n @combine_all\n" ;
print "\nList of files with same extension\n @same";
print "\nList of files with abc only\n @abc_only";
print "\nList of files with def only\n @def_only";
foreach my $item (sort @combine_all) {
print FILE2 "$item\n" ;
}
close (FILE2) ;
My output is like this which is wrong:-
1st:- print screen output as below:
Combine all:-
file.abc file.abc file1 file10def.abc file2 file3 file4.abc file5 file6.def file7.abc
List of files with same extension
file1 file2 file3 file5
List of files with abc only
file4.abc file.abc file7.abc file.abc file10def.abc
List of files with def only
file6.def
Log output as below:
**file.abc
file.abc**
file1
file10def.abc
file2
file3
file4.abc
file5
file6.def
file7.abc
Can you pls help me take a look where gies wrong? Thanks heaps.
Output is
Hope this helps…