how to grep between two lines in perl
I have got 1 file which looks like this
COMPANY=xyz
system.employee[0].Name=shayam
system.employee[0].Age=26
system.employee[0].sex=Male
system.employee[1].Name=ram
system.employee[1].Age=28
system.employee[1].sex=Male
COMPANY=bhg
system.employee[0].Location=Bangalore
system.employee[0].Number=26
system.employee[1].Location=chennai
system.employee[1].Number=28
I want to form a table with company xyz with columns Name , Age, sex and one more table with columns Location & Number. Can开发者_开发百科 you help me how to acheive this using perl The output should be like this
COMPANY xyz Name Age Sex Shayam 26 Male Ram 28 Male COMPANY bgh Location Number Bangalore 26 Chennai 28
Thanks
The rough code below does a progressive match i.e. it moves down the string to extract the data you want. Similar code could be used for the second part of your file:
#!/usr/bin/perl -w
use strict;
use warnings;
my $inline;
{
local $/ = undef; #turn on slurp mode
$inline = <DATA>;
}
print "COMPANY xyz\nName Age sex\n"; #Report header
$inline =~ s/^COMPANY=xyz\n(.*)COMPANY=bhg/$1/msx; #strip off surplus text to
$inline =~ s/system\.employee\[\d+\]\.//gx; #simplify progressive match below
while (
$inline =~ /
(?: #start of non capturing block
Name=(.*)\n
Age=(.*)\n
sex=(.*)\n
){1} # end of non-capturing block (progressive match)
/gmx # g=progressive match, m=multiline match x=comments
)
{
print "$1 $2 $3\n";
}
__DATA__
COMPANY=xyz
system.employee[0].Name=shayam
system.employee[0].Age=26
system.employee[0].sex=Male
system.employee[1].Name=ram
system.employee[1].Age=28
system.employee[1].sex=Male
COMPANY=bhg
system.employee[0].Location=Bangalore
system.employee[0].Number=26
system.employee[1].Location=chennai
system.employee[1].Number=28
精彩评论