下面是单个列的输出,其中重复行可以是正则表达式/拆分等的一部分。
我想将分组列转换为逗号分隔格式。有人可以帮我这个吗?
在:
An instance of HostInfo
1=?
2=?
3=?
4=?
5=?
An instance of HostInfo
1=?
2=?
3=?
4=?
5=?
在
1, 1=?, 2=?, 3=?, 4=?, 5=?
2, 1=?, 2=?, 3=?, 4=?, 5=?
答案 0 :(得分:3)
应该记住,Perl中的 line 处理是记录处理的一个实例。您可以将记录分隔符设置为适合您数据的内容。
假设文件 包含字符串“HostInfo的实例”,您可以执行以下操作。
您还可以设置记录分隔符:
use English qw<$RS>;
my $old_rs = $RS;
local $RS = "An instance of HostInfo\n";
然后您可以在那些块中读取文件。
while ( <$input> ) {
chomp; # removes record separator
next unless $_;
...
}
然后您可以将记录拆分为行并使用逗号重新加入。所以...
是:
say join( ', ', split $old_rs );
答案 1 :(得分:0)
这样的事情会起作用吗?
use strict;
use warnings;
undef $/;
my $output = <DATA>;
my @parts = split /An instance of HostInfo/m, $output;
my $ctr = 1;
for my $part (@parts) {
my @lines = split "\n", $part;
@lines = grep {$_} @lines;
next unless @lines;
s/^\s+//g for @lines;
s/\s+$//g for @lines;
print $ctr++, ', ', join(", ", @lines),"\n";
}
__DATA__
An instance of HostInfo
1=?
2=?
3=?
4=?
5=?
An instance of HostInfo
1=?
2=?
3=?
4=?
5=?
这会将您的示例输出读取为单个字符串,并将其拆分为“HostInfo的实例”, 然后在每个段上循环,分割线条,修剪它们,最后将它们连接在一起。
答案 2 :(得分:0)
尝试这样做:
use strict; use warnings;
my ($count, $hash);
# magic diamond operator to read INPUT
while (<>) {
# removes newlines
chomp;
# if the line contains /An instance/
# incrementing $count and skipping this line
do{ $count++; next } if /An instance/;
# else add current line in a reference to an array
push @{ $hash->{$count} }, $_;
}
# iterating over "instances"
foreach my $first_level (sort keys %$hash) {
# finally we print the result by de-referencing the HASH ref
print "$first_level ", join ", ", @{ $hash->{$first_level} }, "\n";
}
USAGE :
perl script.pl < input_file.txt