Perl:修剪多行记录

时间:2012-03-23 23:44:41

标签: perl

我需要阅读多行记录并将其修剪为恰好40行。然后 将它们填充为45行。它们可能大到70 +行。这些记录需要 最终成为45行。

记录分隔符是以模式/ ^ #matchee /.

开头的行

我假设您将$ /设置为#matchee。

{
    $/ = "#matchee";

    while (<>) {
        # I need to print first 40
        # lines of each record then
        # pad to 45 with delimiter as
        # last line.
    }
}

样本记录

REDUNDANCY DEPARTMENT
Anonymous Ave

Item 1
Item 2



<bunch of blank lines>
#matchee

2 个答案:

答案 0 :(得分:1)

这是我的解决方案......

#! /usr/bin/env perl
use strict;
use warnings;

{
    $/ = "#matchee";

    while (my @line = split "\n", <> ) {

    # print first 40 lines of record
        for my $counter (0..39) {
             print($line[$counter] . "\n");
        }

        # pad record with four extra blank lines
        # (last record already ends with a newline)
        print "\n" x 4;
    }
}

+1使用$/ = "#matchee";

这不太正确......第一条记录有45行,第二条记录有44行。

答案 1 :(得分:1)

您指定“记录分隔符是以模式/ ^ #matchee /开头的行。”这有点使记录分离变得复杂,因为$/a special string, but not a regex。您没有指定输出是否使用相同的记录分隔符,但我假设如此。这是一种似乎有效的方法。

#!/usr/bin/env perl
use strict;
use warnings;

sub take_and_pad_lines {
  my ($str, $take, $pad) = @_;

  my @lines = (split(/\n/, $str))[0..$take-1];
  return join "\n", @lines, ('') x ($pad - $take);
}


{
  $/ = "#matchee";

  while (my $record = <> ) {
    # because RS is really begins-with we must clean up first line
    # and double check last record
    unless (1 == $.) {
      $record =~ s/\A.*\n//m;
      last if eof() && $record eq '';
    }

    print take_and_pad_lines( $record, 40, 45 ), "\n";
    print "$/\n" unless eof();
  }
}