加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

Perl读取文本格式化后写入文本

发布时间:2020-12-16 00:04:30 所属栏目:大数据 来源:网络整理
导读:demo: #!/usr/bin/perl -w#Perl pragma to restrict unsafe constructsuse strict;use utf8;#main functionsub main { #get params # @_ # Within a subroutine the array @_ contains the parameters passed to that subroutine. # Inside a subroutine,@_


demo:

#!/usr/bin/perl -w
#Perl pragma to restrict unsafe constructs
use strict;
use utf8;

#main function
sub main {
    #get params
    # @_  
    # Within a subroutine the array @_ contains the parameters passed to that subroutine. 
    # Inside a subroutine,@_ is the default array for the array operators push,pop,shift,and unshift.
	# set source file location
    my $s_file = "example_file.txt";
	# set destinate file location
    my $d_file = "file_1.txt";
	# notice and exit if $s_file or $d_file is null
    die "must have source file and destination file!n" unless $s_file && $d_file;

    #open file for read,FO file handler
    if ( open(FO,$s_file) ) {
        if ( open(FOO,">$d_file") ) {
            #do while loop 
            while(<FO>) {
                # $_ general variables 
                my $line = $_;
                # remove head and tail blank
                $line =~ s{^s|s$}{}g;
                # use,split line
                # split /PATTERN/,EXPR. use what split depend on your file delimiter
                my @items = split/,/,$line;
                # write some item to new file
                print FOO  $items[1] . "|" . $items[0] . "  ";
            }
            # close file handler
            close(FOO);
        } else {
			# open file fail
            print "open destination file $d_file errorn";
        }
		# close file handler
        close(FO);
    } else {
		# open file fail
        print "open source file $s_file errorn";
    }
}

# pass params to main function,# @ARGV
# The array @ARGV contains the command-line arguments intended for the script.

main(@ARGV);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读