There is such a text file, the content has many lines as follows, the quantity is indefinite.
Lif (__AMSCRIPT_CD ("Www.jb51.net")) {__AMSCRIPT_WC (' #closead {display:none;} ');
Lif (__AMSCRIPT_CD ("Www.jb51.net")) {__AMSCRIPT_WC (' #footer_win {display:none;} ');
Lif (__AMSCRIPT_CD ("Www.jb51.net")) {__AMSCRIPT_WC ('. Mainad {display:none;} ');
Lif (__AMSCRIPT_CD ("Www.jb51.net")) {__AMSCRIPT_WC ('. mt5.recommend {display:none;} ');
Lif (__AMSCRIPT_CD ("Jbxue.net")) {__AMSCRIPT_WC ('. Ggad {display:none;} ');
Lif (__AMSCRIPT_CD ("Jbxue.net")) {__AMSCRIPT_WC ('. ggsidebox {display:none;} ');
............
Requirements merged into:
Lif (__AMSCRIPT_CD ("Www.jb51.net")) {__AMSCRIPT_WC (' #closead, #footer_win,. Mainad,. mt5.recommend {display:none;} ');
Lif (__AMSCRIPT_CD ("Jbxue.net")) {__AMSCRIPT_WC ('. Ggad,. ggsidebox {display:none;} ');
Thought: The URL can be treated as a key, and the merged string is treated as value, so it is stored and printed. It's just a bit of a hassle to print, because the string contains single quotes, double quotes, parentheses, and curly braces, with q# #做为字符串界定符即可.
Copy Code code as follows:
#!/usr/bin/perl
Use strict;
Use warnings;
Sub Test {
My%comments_of_url = ();
Open FILE, "<d:/codesnippets/perl/abc.txt" or Die $!;
while (<FILE>) {
# Skip Empty lines
Next if/^\s*$/;
# Use URLs as key and #xxx as value for each line
# Merge all #xxx for a URL
if (/amscript_cd\ (. *?) " \) \ {__amscript_wc\ (. *?) \s+\{/) {
$comments _of_url{$. = ($. ',' );
}
}
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 ();