[更新] 使用 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可能是正则表达式!