開発環境
- macOS Mojave - Apple (OS)
- Emacs (Text Editor)
- Windows 10 Pro (OS)
- Strawberry Perl (WindowsのPerlの言語処理系)
- Visual Studio Code (Text Editor)
- Perl 5.28 (プログラミング言語)
初めてのPerl 第7版 (Randal L. Schwartz(著)、brian d foy(著)、Tom Phoenix(著)、近藤 嘉雪(翻訳)、嶋田 健志(翻訳)、オライリージャパン)の9章(正規表現によるテキスト処理)、9.6(練習問題)2の解答を求めてみる。
コード
#!/usr/bin/env perl
use strict;
use warnings;
use v5.28;
say '2.';
my $filename = shift @ARGV;
open my $in_fh, '<', $filename or die $!;
open STDOUT, '>', "$filename.out" or die $!;
while (<$in_fh>) {
s/Fred/Larry/ig;
print;
}
入出力結果(Zsh、PowerShell、Terminal)
% cat sample.txt
Fred
frederick
Alfred
fred flintstone
Mr. Slate
Fred
Mississippi
Bamm-Bamm
llama
wilma
fred
FRED
wilma fred
fred wilma
wilmafred
WilmaFred
barney
Mrs. Wilma flintstone
I saw Wilma Yesterday
I, Wilma!
Fred
fredfredfred
fredfredbarney
barneyfredfred
barneybarneyfred
% ./sample2.pl sample.txt
2.
% cat sample.txt
Fred
frederick
Alfred
fred flintstone
Mr. Slate
Fred
Mississippi
Bamm-Bamm
llama
wilma
fred
FRED
wilma fred
fred wilma
wilmafred
WilmaFred
barney
Mrs. Wilma flintstone
I saw Wilma Yesterday
I, Wilma!
Fred
fredfredfred
fredfredbarney
barneyfredfred
barneybarneyfred
% cat sample.txt.out
Larry
Larryerick
AlLarry
Larry flintstone
Mr. Slate
Larry
Mississippi
Bamm-Bamm
llama
wilma
Larry
Larry
wilma Larry
Larry wilma
wilmaLarry
WilmaLarry
barney
Mrs. Wilma flintstone
I saw Wilma Yesterday
I, Wilma!
Larry
LarryLarryLarry
LarryLarrybarney
barneyLarryLarry
barneybarneyLarry
%
0 コメント:
コメントを投稿