zoukankan      html  css  js  c++  java
  • perl 读取一个文件 替换文件的关键词 把数据替换到新的文件

    replace

    # replace
    #!/usr/bin/perl
    
    my @data = ();
    my ($fname ,$rp, $nfname)= @ARGV;
    my ($o, $n) = split("/", $rp);
    
    open(of, "<$fname") or die "$fname 文件打开失败!$!";
    while(<of>){
        chomp;
        $_ =~ s/$o/$n/;
        push @data, "$_
    ";
    }
    
    if(@data != 0) {
        chomp @data; # 砍掉最后的 
    
        open(nf_handle, ">$nfname") or die "Error: 文件$nfname打开失败$!";
        print nf_handle @data;
    }
    

    执行

    perl replace text.txt world/ajanuw ajanuw.txt
    

    text.txt

    hello world
    
    world asd
    
    123world456
    
    231
    

    ajanuw.txt

    hello ajanuw
    
    ajanuw asd
    
    123ajanuw456
    
    231
    

    改变源文件

    # replace
    #!/usr/bin/perl
    
    my @data = ();
    my ($fname ,$rp)= @ARGV;
    my ($o, $n) = split("/", $rp);
    
    open(rf, "<$fname") or die "$fname 文件打开失败! $!";
    while(<rf>){
        chomp;
        $_ =~ s/$o/$n/;
        push @data, "$_
    ";
    }
    
    if(@data != 0) {
        chomp @data; # 砍掉最后的 
    
        open(wf, "+>$fname") or die "$!";
        print wf @data;
    }
    
  • 相关阅读:
    get和post区别
    cookie和session的区别
    节流和防抖
    eval()
    三次握手和四次挥手
    HTTP状态码
    AMD规范与CMD规范的区别?
    深拷贝和浅拷贝
    逆人性的人类出现,正常合理竞争该何去何从
    TestNG并发执行测试总结
  • 原文地址:https://www.cnblogs.com/ajanuw/p/9275371.html
Copyright © 2011-2022 走看看