2011-01-31 3 views
0

각 행의 처음과 끝에 뭔가를 추가하려는 일부 문서가 있습니다. 원본 문서는 다음과 같습니다 : 나는 단지이으로 바꿀 수있는 다음과 같은 펄 스크립트를 사용하여왜이 줄 바꾸기가 각 줄의 끝을 포함하여 줄의 시작 부분에 더 많이 추가됩니까?

put 'firstLine'; 
put 'secondline'; 

:

firstLine 
secondline 

나는이로 돌려 원하는

put 'firstLine'; 
';put 'secondline'; 

첫 번째 줄 끝과 두 번째 줄 시작 부분에 $이있는 것으로 보입니다. 누군가가 다음 Perl 스크립트의 문제점을 파악하는 데 도움을 줄 수 있습니까?

use File::Find; 
use strict; 

my ($filename, @lines, $oldterm, $newterm); #,$File::Find::name); 
my $dir = "."; 

open MYFILE, ">error.txt" or die $!; 
find(\&edits, $dir); 

sub edits() { 
    $filename = $File::Find::name; 
    if (grep(/\.txt$/, $filename)) {   #only process the perl files 

     # open the file and read data 
     # die with grace if it fails 
     open(FILE, "<$filename") or die "Can't open $filename: $!\n"; 
     @lines = <FILE>; 
     close FILE; 

     # open same file for writing, reusing STDOUT 
     open(STDOUT, ">$filename") or die "Can't open $filename: $!\n"; 

     # walk through lines, putting into $_, and substitute 2nd away 
     for (@lines) { 
      s/(&.+)/' "$1" '/ig; 
      s/^/put '/ig; 
      s/$/';/ig; 
      print; 
     } 

     #Finish up 
     close STDOUT; 
    } 
} 

답변

1

한 단계로 수행하면 행운을 빕니다. 뭔가 같은 :

s/^(&.+)$/put '$1';/im; 
8

모두에서 정규 표현식을 사용하지 마십시오

for (@lines) { 
    chomp; # remove newline at the end of the implicit variable $_ 
    print "puts '$_'\n"; 
} 
: 이미 @lines 배열에서 분리 된 라인을 가지고
관련 문제