perl用普通括号替换花括号

时间:2012-09-03 15:28:11

标签: perl replace

我需要编写一个查看文件的脚本,并用普通括号替换任何花括号。到目前为止我有:

use strict;
use warnings;
open(INFILE,"<rscore") || die "Couldn't open rscore for reading!\n";
open(OUTFILE,">rscore.new") || die "Couldn't open rscore.new for writing!\n";
while(<INFILE>){
  $_ =~ s/{/(/gi; #g for every occurrence, i for case-insensitive
  print OUTFILE $_;
}
close INFILE;
close OUTFILE;
rename("rscore.new","rscore") || die "Couldn't rename the new file!\n"; 

并收到以下错误:

syntax error near line 10 near insensitive print.

这可能是愚蠢的事。此外,如果有更有效的方式(我确信有),我会接受建议。

3 个答案:

答案 0 :(得分:5)

这是一个perl单行。

$ perl -lpe 'tr |{}|()|' <infile >outfile

答案 1 :(得分:3)

请改为尝试:

$string =~ tr/{}/()/;

答案 2 :(得分:1)

这是一个perl one liner,用于原位编辑文件

perl -pi -e 's/{/(/g;s/}/)/g' your_file

下面将在控制台上打印输出

perl -p -e 's/{/(/g;s/}/)/g' your_file