There is such a text file with multiple lines of content as follows, and the quantity is uncertain.
Lif(__amscript_cd("")){__amscript_wc('#closead {display:none;}');};
Lif(__amscript_cd("")){__amscript_wc('#footer_win {display:none;}');};
Lif(__amscript_cd("")){__amscript_wc('.mainad {display:none;}');};
Lif(__amscript_cd("")){__amscript_wc('. {display:none;}');};
Lif(__amscript_cd("")){__amscript_wc('.ggAD {display:none;}');};
Lif(__amscript_cd("")){__amscript_wc('.ggSideBox {display:none;}');};
…………
Required to merge as:
Lif(__amscript_cd("")){__amscript_wc('#closead, #footer_win, .mainad, . {display:none;}');};
Lif(__amscript_cd("")){__amscript_wc('.ggAD, .ggSideBox {display:none;}');};
Idea: You can treat the url as a key and the merged string as a value, so you can store it and print it. It's just a bit troublesome when printing, because this string contains single quotes, double quotes, small brackets and curly brackets, and use q## as string delimiters.
#!/usr/bin/perl
use strict;
use warnings;
sub test {
my %comments_of_url = ();
open FILE, "<D:/Codesnippets/Perl/" or die $!;
while (<FILE>) {
# Skip empty lines
next if /^\s*$/;
# Use url as key and #xxx as value for each line
# Merge all the #xxx for a url
if (/amscript_cd\("(.*?)"\)\){__amscript_wc\('(.*?)\s+\{/) {
$comments_of_url{ $1 } .= ( $2 . ',' );
}
}
foreach my $key (keys %comments_of_url) {
chomp (my $value = $comments_of_url{$key});
print q{Lif(__amscript_cd("};
print $key;
print q#")){__amscript_wc('#;
print $value;
print q#{display:none;}');};#;
print "\n";
}
}
sub main {
&test();
}
&main();