2012年6月9日土曜日

開発環境

『初めてのPerl 第5版』(Randal L. Schwartz, Tom Phoenix, brian d foy 共著、近藤 嘉雪 訳、オライリー・ジャパン、2009年、ISBN978-4-87311-427-9) の9章(正規表現によるテキスト処理), 9.6(練習問題)2を解いてみる。

2.

やり方の1つ。(「やり方は何通りもある」(TIMTOWTDI(There Is More Than One Way To Do It.)))

コード(TextWrangler)

#!/usr/bin/env perl
use strict;
use warnings;

my $in = $ARGV[0];
my $out = $in;
$out =~ s/(\.\w+)?$/.out/;
open IN, "<$in";
open OUT,">$out";
while(<IN>){
  s/fred/larry/ig;
  print OUT;
}

入出力結果(Terminal)

 
$ cat test
A
a
kamimura's blog
http://sitekamimura.blogspot.com
KMI
http://www.mkamimura.com   
Fred
FRED
fred
frederick
Alfred
fred flintstone
Mr. Slate     
Mississippi
Bamm-Bamm    
wilama
barney
wilma and fred
fred and wilma
Wilma and Fred      
Fred and Wilma
wilma&fred
Mrs. Wilma Flintstone   
I saw Wilma yesterday
I, Wilma!
Z
llama
$ cat test.out
A
a
kamimura's blog
http://sitekamimura.blogspot.com
KMI
http://www.mkamimura.com   
larry
larry
larry
larryerick
Allarry
larry flintstone
Mr. Slate     
Mississippi
Bamm-Bamm    
wilama
barney
wilma and larry
larry and wilma
Wilma and larry      
larry and Wilma
wilma&larry
Mrs. Wilma Flintstone   
I saw Wilma yesterday
I, Wilma!
Z
llama
$

0 コメント:

コメントを投稿