Example code of Perl text Merging

Source: Internet
Author: User

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 ();
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.