開発環境
- 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(練習問題)4の解答を求めてみる。
コード
#!/usr/bin/env perl
use strict;
use warnings;
use v5.28;
say '4.';
$^I = '.bak';
while (<>) {
s/(#!.*)/$1\n## Copyright ©️ 2020 by kamimura/;
print;
}
入出力結果(Zsh、PowerShell、Terminal)
% ls dir4_1
./ sample1.pl* sample3.pl*
../ sample2.pl* sample4.pl*
% head -n 2 dir4_1/*.pl
==> dir4_1/sample1.pl <==
#!/usr/bin/env perl
use strict;
==> dir4_1/sample2.pl <==
#!/usr/bin/env perl
use strict;
==> dir4_1/sample3.pl <==
#!/usr/bin/env perl
use strict;
==> dir4_1/sample4.pl <==
#!/usr/bin/env perl
use strict;
% ./sample4.pl dir4_1/*.pl
4.
% head -n 3 dir4_1/*.pl
==> dir4_1/sample1.pl <==
#!/usr/bin/env perl
## Copyright ©️ 2020 by kamimura
use strict;
==> dir4_1/sample2.pl <==
#!/usr/bin/env perl
## Copyright ©️ 2020 by kamimura
use strict;
==> dir4_1/sample3.pl <==
#!/usr/bin/env perl
## Copyright ©️ 2020 by kamimura
use strict;
==> dir4_1/sample4.pl <==
#!/usr/bin/env perl
## Copyright ©️ 2020 by kamimura
use strict;
% ls dir4_2
./ sample1.pl* sample3.pl*
../ sample2.pl* sample4.pl*
% head -n 2 dir4_1/*.bak
==> dir4_1/sample1.pl.bak <==
#!/usr/bin/env perl
use strict;
==> dir4_1/sample2.pl.bak <==
#!/usr/bin/env perl
use strict;
==> dir4_1/sample3.pl.bak <==
#!/usr/bin/env perl
use strict;
==> dir4_1/sample4.pl.bak <==
#!/usr/bin/env perl
use strict;
% ls dir4_2/
./ sample1.pl* sample3.pl*
../ sample2.pl* sample4.pl*
% head -n 2 dir4_2/*.pl
==> dir4_2/sample1.pl <==
#!/usr/bin/env perl
use strict;
==> dir4_2/sample2.pl <==
#!/usr/bin/env perl
use strict;
==> dir4_2/sample3.pl <==
#!/usr/bin/env perl
use strict;
==> dir4_2/sample4.pl <==
#!/usr/bin/env perl
use strict;
% perl -p -i.bak -w -e 's/(#!.*)/$1\n## Copyright 2020 by kamimura' dir4_2/*.pl
Substitution replacement not terminated at -e line 1.
% perl -p -i.bak -w -e 's/(#!.*)/$1\n## Copyright (C) 2020 by kamimura/' dir4_2/*.pl
% ls dir4_2
./ sample1.pl* sample2.pl* sample3.pl* sample4.pl*
../ sample1.pl.bak* sample2.pl.bak* sample3.pl.bak* sample4.pl.bak*
% head -n 3 dir4_2/*
==> dir4_2/sample1.pl <==
#!/usr/bin/env perl
## Copyright (C) 2020 by kamimura
use strict;
==> dir4_2/sample1.pl.bak <==
#!/usr/bin/env perl
use strict;
use warnings;
==> dir4_2/sample2.pl <==
#!/usr/bin/env perl
## Copyright (C) 2020 by kamimura
use strict;
==> dir4_2/sample2.pl.bak <==
#!/usr/bin/env perl
use strict;
use warnings;
==> dir4_2/sample3.pl <==
#!/usr/bin/env perl
## Copyright (C) 2020 by kamimura
use strict;
==> dir4_2/sample3.pl.bak <==
#!/usr/bin/env perl
use strict;
use warnings;
==> dir4_2/sample4.pl <==
#!/usr/bin/env perl
## Copyright (C) 2020 by kamimura
use strict;
==> dir4_2/sample4.pl.bak <==
#!/usr/bin/env perl
use strict;
use warnings;
%
0 コメント:
コメントを投稿