Chinaunix首页 | 论坛 | 博客
  • 博客访问: 858739
  • 博文数量: 253
  • 博客积分: 6891
  • 博客等级: 准将
  • 技术积分: 2502
  • 用 户 组: 普通用户
  • 注册时间: 2010-11-03 11:01
文章分类

全部博文(253)

文章存档

2016年(4)

2013年(3)

2012年(32)

2011年(184)

2010年(30)

分类: Python/Ruby

2011-08-31 13:56:58

 $^I. Bydefault it’s undef, and everything is normal. But when it’s set to some string, it makes the diamond operator (<>) even more magical than usual.
Let’s say it’s time for the diamond to open our file fred03.dat. It opens it like before,
but now it renames it, calling it fred03.dat.bak.* We’ve still got the same file open, but
now it has a different name on the disk. Next, the diamond creates a new file and gives
it the name fred03.dat. That’s okay; we weren’t using that name any more. And now
the diamond selects the new file as the default for output, so that anything that we print
will go into that file.† So now the while loop will read a line from the old file, update
that, and print it out to the new file. This program can update thousands of files in a
few seconds on a typical machine.

现有一文件test.txt,要在指定某行前加入一些内容。


  1. #!/usr/bin/perl -w

  2. use strict;
  3. $^I = ".bak";

  4. sub find_position{

  5.     my $lineno = 0;
  6.     my $newcomp = get_comp_xml("test", "url");

  7.     while(<>){
  8.         if (not /<\/component_list>/){
  9.             print;
  10.         }else{
  11.             print $newcomp;
  12.             print "\n";
  13.             print;
  14.         }
  15.     }
  16. }

  17. sub get_comp_xml{
  18. #my $file = shift;
  19.     my $comp = shift;
  20.     my $url = shift;

  21. #$file =~ /(check|skip|full|official|klocwork)/i;
  22. # my $status = $1;

  23.     my $string = "
  24.     
  25.     
  26.    ";
  27.     return $string;

  28. }

  29. system("dos2unix $ARGV[0]");

  30. find_position(@ARGV);
这时就会生成新的test.txt原来的文件备份成test.txt.bak.
这样做的不好是我只能向脚本转入要修改的文件,要是想把参数传给脚本就不行了。

下面做了一点点改动,从文件里得到想要的参数。
  1. #!/usr/bin/perl -w

  2. use strict;
  3. $^I = ".bak";

  4. sub find_position{

  5.     my $newcomp = get_comp_xml(@_);

  6.     while(<>){
  7.         if (not /<\/component_list>/){
  8.             print;
  9.         }else{
  10.             print $newcomp;
  11.             print;
  12.         }
  13.     }
  14. }

  15. sub get_comp_xml{
  16.     my $file = shift;
  17.     my $compfile = 'cominfo.txt';
  18.     my @cominfo;
  19.     my $status;

  20.     open(COMPF, $compfile) or die "Can not open file $compfile\n";

  21.     while(<COMPF>){
  22.         chomp;
  23.         push(@cominfo, $_);
  24.     }
  25.     close(COMPF);

  26.     $file =~ /(check|skip|full|official|klokwork)/i;
  27.     $status = lc($1);

  28.     if($status =~ /full/){
  29.         $status = "build";
  30.     }

  31.     my $string = "
  32.     
  33.     
  34.    \n";
  35.     return $string;

  36. }

  37. system("dos2unix $ARGV[0]");
  38. find_position(@ARGV);
通过两个文件来修改。
  1. #!/usr/bin/perl -w

  2. use strict;

  3. sub find_position{
  4.     my $file = shift;
  5.     my $newfile = $file.".new";

  6.     open(FILE, $file) or die "Cannot open file $file\n";

  7.     if(-f $newfile){
  8.         unlink $newfile;
  9.     }

  10.     open(NEWFILE, ">>$newfile") or die "Cannot create file $newfile\n";
  11.     binmode(FILE);
  12.     binmode(NEWFILE);

  13.     my $newcomp = get_comp_xml(shift, shift);

  14.     while(<FILE>){
  15.         if (not /<\/component_list>/){
  16.             print NEWFILE $_;
  17.         }else{
  18.             print NEWFILE $newcomp;
  19.             print NEWFILE $_;
  20.         }
  21.     }
  22. }

  23. sub get_comp_xml{
  24.     my $comp = shift;
  25.     my $url = shift;

  26.     my $string = "
  27.     
  28.     
  29.    \n";
  30.     return $string;

  31. }

  32. find_position(@ARGV);


阅读(544) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~