1

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

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.

这可能是一件愚蠢的事情。此外,如果有更有效的方法来做到这一点(我确信有),我愿意接受建议。

4

3 回答 3

5

这是一个 perl 单行代码。

$ perl -lpe 'tr |{}|()|' <infile >outfile
于 2012-09-03T15:33:27.563 回答
3

试试这个:

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

于 2012-09-03T15:32:55.173 回答
1

This is a perl one liner for inplace editing the file

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

This below will print the output on console

perl -p -e 's/{/(/g;s/}/)/g' your_file
于 2012-09-03T18:10:30.670 回答