perl - How to match lines in one file against lines in another? -
i know majorly wrong here, new perl , looking following:
find lines in all.css contain lines in unused.css , logic. way code structured, seems cannot match like:
if ($linea =~ /$lineu/) #if line in all.css contains line in unused.css
since variables being defined separately.
how structure program able match lines in all.css against lines in unused.css?
my program below:
#!/usr/bin/perl use strict; use warnings; open(my $unused_handle,'<', "unused.css") or die $!; open(my $all_handle,'<',"all.css") or die $!; open(my $out, '>' ,'lean.css') or die $!; $lineu = q{}; $linea = q{}; print $out "$lineu"; while($lineu =<$unused_handle>) { print $out "$lineu"; #print $out "$linea"; line 1 not printed while($linea =<$all_handle>) { if ($linea =~ m/$lineu/sxm) { print "huza!\n"; } else { print "no match\n"; } } } close ($unused_handle); close ($all_handle); close ($out); print "done!\n"; exit;
an example of input files below.
example lines unused.css:
audio, canvas, video audio:not([controls]) [hidden] h6
example lines all.css:
article, aside, details, figcaption, figure, footer, header, hgroup, nav, section, summary { display: block; } audio, canvas, video { display: inline-block; *display: inline; *zoom: 1; } audio:not([controls]) { display: none; height: 0; } [hidden] { display: none; }
i hope (untested) snippet helps bit:
#!/usr/bin/perl use strict; use warnings; open(my $unused,'<', "unused.css") or die $!; open(my $all,'<',"all.css") or die $!; # store lines of unused.css in hash %unused_line; while (<$unused>) { #remove newlines chomp(); #store line key empty value %unused_line{$_}=""; } close ($unused); #...for every line in all.css while (<$all>) { #have seen in unused.css (at least before ' {') if ((m/^(.*\s+)\{/) && (exists $unused_line{$1})) { #a match - found line of unused.css in all.css }else{ #no match - line not exists in unused.css } } close ($all);
Comments
Post a Comment