There is a text file with multiple lines as follows, and the quantity is not fixed.
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 ;}');};
............
Merge as follows:
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 ;}');};
Idea: You can regard the url as the key, and the merged string as the value. Store it and print it. It is a little troublesome to print, because the string contains single quotation marks, double quotation marks, small arc and curly arc. Use q # As the string separator.
Copy codeThe Code is 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 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 ();