前に、タスクの特定の言語が表示されず、について読むのが遅すぎましたc#
。これがperl
解決策ですが、よくコメントされているので、便利で他の言語に簡単に翻訳できることを願っています.
infile
次のようなテスト ファイル ( ) を想定します。
1
2
3
4
5
#Adm
6
7
#Prov
8
9
#Adm
10
11
#Prov
12
13
#Adm
14
15
#Prov
16
17
の内容script.pl
:
use warnings;
use strict;
## Assign empty value to read file by paragraphs.
$/ = qq[];
## Arrays to save second row of its section.
my (@adm, @prov);
## Regex to match beginning of section.
my $regex = qr/(?:#(?|(Adm)|(Prov)))/;
## Read file.
while ( <> ) {
## Remove last '\n'.
chomp;
## If matches the section and it has at least two lines...
if ( m/\A${regex}/ and tr/\n/\n/ == 2 ) {
## Group the section name ($1) and its second line ($2).
if ( m/\A${regex}.*\n^(.*)\Z/ms ) {
## Save line in an array depending of section's value.
if ( $1 eq q[Adm] ) {
push @adm, $2;
}
elsif ( $1 eq q[Prov] ) {
push @prov, $2;
}
}
}
}
## Print first lines of 'Adm' section and later lines of 'Prov' section.
for ( ( @adm, @prov ) ) {
printf qq[%s\n], $_;
}
exit 0;
次のように実行します。
perl script.pl infile
次の出力で:
7
11
15
9
13
17