[更新] chomp を使用すると$_、これがさらに短くなります。
これはそれを行う必要があります:
入力レコードセパレーターが 21-のシーケンスである場合、これは次の方法で簡単perl -neです。
perl -ne 'BEGIN{ $/=("-"x21)."\n"; $i=0; }
do { open F, ">file".($i++);
chomp;
print F;
close F;
} if /^Section/' yourfile.txt
動作し、ファイルを作成する必要がありますfile0.. fileN.
説明
おそらく、スタンドアロンの Perl スクリプトとして説明する方が簡単でしょうか?
$/=("-"x21)."\n"; # Set the input-record-separator to "-" x 21 times
my $i = 0; # output file number
open IN, "<yourfile.txt" or die "$!";
while (<IN>) { # Each "record" will be available as $_
do { open F, ">file".($i++);
chomp; # remove the trailing "---..."
print F; # write the record to the file
close F; #
} if /^Section/ # do all this only it this is a Section
}
ここではPerl のawk系譜が役に立ちましたので、比較のためにawkバージョンを示しましょう。
awk 'BEGIN{RS="\n-+\n";i=0}
/Section/ {chomp; print > "file_"(i++)".txt"
}' yourfile.txt
バージョンに比べて悪くはありませんがperl、実際には短くなっています。$/Perl の は の変数RSですawk。ここでは awk が優勢です:RS正規表現かもしれません!