:あなたの宿題の特殊な状況では
perl -pe "s/new/old/gi" WK5input.txt > output.txt
は、しかし、あなたは合理的な目標に近いが、多くなりました少しの細かい部分を修正しなければならなかった。
(これらの詳細、大文字と小文字の区別がありませんでしたが、適切なサンプル入力と希望の出力を提供していれば、それは起こりませんでした)[doc] [mcve]を読んでみてください)
私は効率的なソリューションを作るために特別な副要件があなたの課題と通常の目標とを区別するための前提条件。
私はあなたのバージョンの作業を上記の最適化されたバージョンよりも役立つ小さな変更があると考えているので、自分のバージョンにできるだけ近いところに解決策を提供します。それはあなたがどれくらい近づいているかを完全に隠すほどコンパクトです。
完全に最適化されておらず、宿題指向でもない中間バージョンのその他の回答を確認してください。 最適化されたソリューションでは、perlをスキップしてawkを使用したり、脳の歴史的な運動の目的でsedを使用する人もいます。
これらは、私はあなたが従うことが期待されていると思います
暗黙のルールには、いくつかの仮定を、以下のとおりです。
- はありません、すべての要件をカバーするPerlプログラムを作成し、シェルの機能を用いないすなわち
他のツール
- は、コマンドラインパラメータ
を使用していない(あなたの試みでなかったからといって、多分あなたはまだあなたのコースでそれらをカバーしていない)ラインproceによって
- 使用ラインssing
(ちょうどあなたの試みでそうしたからです。あなたのコースの他の方法をまだカバーしていないかもしれません)
- "あいまいなperl magics"を使わないでください。これらの推測ルールのいずれかがあなたの譲渡には適用されない場合、彼らはそれ以外の
(「なしシェル」ルールの特殊なケース/通訳)利便性が高いにもかかわらずコマンドラインオプションは、
をしている-pe`他の答えを見てください。彼らは興味深い選択肢を提供します。
# nice touch, using these is very good practice
use strict;
use warnings;
# Not necessary, but good practice: collect the "my"s in one place soon.
# This supports self-documenting inside the code.
# Doing it with the "my" at the first use is an aleternative option and preferred by some.
my $filename = 'WK5input.txt'; # file name for the input file
my $fhin; # file handle for the input file
my $fhout; # file handle for the output file
my $row; # variable with currently processed line
# Prefer to use the three parameter version of "open", explicitly stating the mode.
open($fhin, '<', $filename) # two file handles are needed, use different names
or die 'Could not open input file "'.$filename.'" '.$!;
# I chose to concatenate both variables (file name and failure reason) explicitly,
# to some text inside '...', which can be more efficiently handled by perl interpreter.
# This saves work on text inside "..." and is more self explaining, i.e. it is easier
# to understand at the first reading what the code does.
# The second file handle is setup here, to read from input and write to output at the same time.
open($fhout, '>', 'output.txt')
or die 'Could not open output file output.txt '.$!;
while ($row = <$fhin>) { # you are reading into a dedicated variable here ...
# There was the code "chomp $row;" here.
# This removes the newline from ther end of the line, if there is one.
# It is not needed if you are going to append the newline again before printing.
$row =~ s/new/old/gi; # ... you need to use the variable here, instead of "$_"
# The additional "i" behind the "g", makes the search for "new", "New", "NEW" case insensitive.
# Credits to other answer and comments for finding the requirement I missed.
# I accept the requirement to replace new->old, though it seems strange.
# I itched to replace old->new.
print $fhout $row; # print into the output file instead of to stdout
# You had an additional "\n" at the end, which was in fact needed, but only
# because of the "chomp" a few lines above.
# Also, you had the variable in quotes, i.e. "$row\n". That uses some time for interpreting
# the text inside the quotes. If you only want to print the content of a variable, then
# only print the variable outside of quotes.
}
# There was the code "$_ =~ s/new/old/g;" here, it was moved into the loop.
# Compare to a different answer to see a solution which used a single global replace on
# a variable with all the input. Instead, I decided to go for line by line processing in
# a loop, because it seemed closer to your approach.
# There was the code "open($fh, '>', 'output.txt');" here it was moved to before the loop.
# There was the code "print $filename ;" here. It was deleted, because it seems not to be
# required by the assignment. Printing the modified content is done line by line inside the
# loop.
# Closing file handles instead of file name:
close $fhin;
close $fhout;
(StackOverflowのが宿題の質問のための完全なソリューションを提供すぐにしないことをお勧めします。
私は「...すでに解決に近くない宿題の質問のため」とすることを解釈する。
私はので、ソリューションを提供
最後の詳細を表示するには、十分に近いものを試してみてください。
StackOverflowは、既に見ている方法でさらに少しずつ学生を取り入れることを推奨しています、便利な方法で。opのみを提供するこの答えの一つの始まりであるタイムリーで微調整された最終版は、それらのために建設的ではありません。
これは本当に私の答えの中に本当に悪いコードの言い訳ではないので、誰もがそれを指摘すること自由に感じる。しかし編集するときは、私の目標に固執して、OPの試行に近づけてください。)
@AruneshSingh '-i'を使用すると、明示的に必要とするOPに矛盾するように見えます。 – Yunnosch
OP、別のperlスクリプトが必要です(例のように)か、コマンドラインの一行も役立ちますか? – Yunnosch
スクリプトからファイルを読み込んで新しいファイルに書き込むか、シェルリダイレクトを使用できますか? – Yunnosch