Perl simple text file processing

Source: Internet
Author: User

On the China Mobile Game channel, the player paradise game in the game player package is now available. As one of the developers, we have added so many classes that we can see the results and are excited. Haha, various PV and UV data statistics follow.

The leader asked: Can I still write Perl?

A: Yes.

After entering this company, Perl scripts are indeed involved, so they can be written in a little simple way. Therefore, I have completed the processing of the access_log logs explained by the leaders and posted them to my blog, for future memories.

This script mainly involves passing parameters when running the Perl Command Line, checking whether the file exists and creating it, and using some simple regular expressions.

#! /Usr/bin/perl </P> <p> # $ filename = "localhost_access_log.2011-06-16.txt"; <br/> # $ outname = "new ". $ filename; OPERATOR. <br/>######## used during testing ####################< br/> # $ outname = "New _ $ FILENAME "; <br/> # Open (file, $ filename) or die "can't open the file: $! /N "; <br/>######## used during testing ###################</P> <p >######## used in official use ###################< br/> # $ argv [0] command line parameters <br/> $ outname = "New _ $ argv [0]"; <br/> open (file, $ argv [0]) or die "can't open the file: $! /N "; <br/>######## used during official use ##################< br/> # Open (OUTFILE, ">>$ outname") or die "can't open the file: $! /N "; <br/> # determine whether the file exists <br/> unless (-e $ outname) <br/>{< br/> Print" file not found. then create it/N "; <br/> # create and write a file <br/> open (OUTFILE,"> $ outname ") or die" couldn't open $ outname: $! "; <Br/>}< br/> else <br/>{< br/> open (OUTFILE,"> $ outname ") or die "couldn't open $ outname: $! "; <Br/> Print" file found. /n "; <br/>}</P> <p> while ($ line = <File>) <br/>{< br/> # Replace [] in the row with null <br/> $ line = ~ S/['/[' | '/]' | '/"'] // G; <br/> # Replace spaces with, <br/> $ line = ~ S/,/g; </P> <p> # remove the last row <br/> $ line = substr ($ line, 0, length ($ line)-1); </P> <p> # string matching/wappetserver <br/> if ($ line = ~ /// Wappetserver/) <br/>{< br/> my @ ROW = Split (//, //, $ line ); <br/> $ row [6] = "/wappetserver/syssetting"; <br/> $ line = join (",", @ row ); <br/>}</P> <p> Print OUTFILE $ line. "/N"; <br/>}< br/> close file; <br/> close OUTFILE;  

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.