我需要读入多行记录并将它们修剪到恰好 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
最佳答案
这是我的解决方案...
#! /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 行。
关于Perl:修剪多行记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9847850/