在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
1, 利用Tie::File模块来直接对文件内容进行修改。 #!/usr/bin/perl -w 2, 直接命令: perl -pi -e 's/(.*[,])$std([,].*)/${1}$fast${2}/g' $file 3, 文件小的话,读取修改后再覆盖进去: sub updatefile3 { open FILE, $file or die "$!"; my @oldfile=<FILE>; close FILE; foreach(@oldfile) { s/,$std,/,$fast,/g; #s/,$fast,/,$std,/g; } open FILE, ">", $file or die "$!"; print FILE @oldfile; close FILE; } 4, 通过创建临时文件方式: sub updatefile1 { if(defined($file)) { my $buffer="$file buffer"; # save the new data my $bak = "$file.bak"; #back up the old data open OPENFILE, "<", "$file" or die "can not open file:$!\n"; open OPENFILEE, ">", "$buffer" or die "can not open file:$!\n"; $^I=".bak"; while(<OPENFILE>) { chomp; s/,$std,/,$fast,/g; #s/,$fast,/,$std,/g; print "$_\n"; print OPENFILEE "$_\n" or die "can not write to $buffer: $!"; } close OPENFILE; close OPENFILEE; rename("$file", "$bak") or die "can not rename $file to $bak: $!"; rename("$buffer", "$file") or die "can not rename $buffer to $file: $!"; } else { print "please provide file name!\n"; } }
5,一行一行的修改: 比如x.txt内容如下: |
请发表评论