Reputation: 14533
I am working on a Perl script that processes the output from ec2-describe-instances which is returning its output as follows:
INSTANCE\n
TAG\n
INSTANCE\n
TAG\n
INSTANCE\n
TAG\n
Is there a way that I can use the split function so that the results are split into two line chunks such as the following?
INSTANCE\n
TAG\n
Upvotes: 0
Views: 176
Reputation: 385655
The first arg of split
should be what separates what you wanted returned.
split /(?!^)(?=(?:.*\n.*\n)+\z)/, do { local $/; <> };
But that's very inefficient.
If you want to output to a handle, you could use:
perl -pe'()=split//,""; print "\n" if $. % 2 == 1 && $. > 1'
Which is similar to:
()=split//,"";
while (<>) {
print "\n" if $. % 2 == 1 && $. > 1;
print;
}
(I strongly recommend leaving out the ()=split//,"";
.)
$.
the line number of the last line read.
If you want to output to an array, you could use:
()=split//,"";
my @array;
my $buf;
while (<>) {
$buf .= $_;
if ($. % 2 == 1) {
push @array, $buf;
$buf = '';
}
}
(I strongly recommend leaving out the ()=split//,"";
.)
If you're ok with loading everything into memory, you could use
()=split//,"";
my @array = do { local $/; <> } =~ /\G.*\n.*\n/g;
(I strongly recommend leaving out the ()=split//,"";
.)
Upvotes: 1