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

全部博文(253)

文章存档

2016年(4)

2013年(3)

2012年(32)

2011年(184)

2010年(30)

分类: Python/Ruby

2011-08-31 10:01:27

需求:
现有一文件,名为test.sh,里面的内容如下export BUILD_DINER_PID="1016"
export TEST1="C:\Users\test"
export TOOL=""C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\devenv.com""
export SOURCE="C:\Users\project"

现要把这个文件的内容改为
set TEST1=C:\Users\test
set TOOL="C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\devenv.com"
set SOURCE=C:\Users\project
并存入test.pl文件里。
  1. #!/usr/bin/perl -w
  2. #
  3. use strict;

  4. our $filename = 'build_env_old.sh';

  5. sub replace_func{
  6.     our $filename;
  7.     (my $newfile = $filename ) =~ s/\.sh/.bat/;
  8.     print $newfile;

  9.     open(FILE, $filename) or die "Cannot open the file\n";

  10.     if (-e $newfile){
  11.         unlink $newfile;
  12.     }

  13.     open(NEWF, ">>$newfile") or die " Cannot create file $newfile\n";
  14.     binmode(NEWF);
  15.     binmode(FILE);

  16.     while( <FILE>){
  17.         s/^export/set/;
  18.         if (not /TOOL/){
  19.             s/"//g;
  20.         }else{
  21.             s/""/"/g;
  22.         }
  23.         print $_;
  24.         print NEWF $_;
  25.     }
  26.     close(FILE);
  27.     close(NEWF);
  28. }

  29. replace_func();
binmode函数把文件转成二进制文件。这样就不会出现windows 操作符 ^M.

A filehandle open for writing or appending may be used with print or printf, appearing
immediately after the keyword but before the list of arguments:
print LOG "Captain's log, stardate 3.14159\n";  # output goes to LOG
printf STDERR "%d percent complete.\n", $done/$total * 100;
Did you notice that there’s no comma between the filehandle and the items to be prin-
ted?* This looks especially weird if you use parentheses. Either of these forms is correct:
printf (STDERR "%d percent complete.\n", $done/$total * 100);
printf STDERR ("%d percent complete.\n", $done/$total * 100);


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