I have a file that contain huge number of net names. I would like to compress the bus nets as below:
abc/def/gh[0]
abc/def/gh[1]
abc/def/gh[2]
ab/ef/xx
abc/def/gh[3]
to
abc/def/gh[3:0]
ab/ef/xx
I have a file that contain huge number of net names. I would like to compress the bus nets as below:
abc/def/gh[0]
abc/def/gh[1]
abc/def/gh[2]
ab/ef/xx
abc/def/gh[3]
to
abc/def/gh[3:0]
ab/ef/xx
The question was not clear. However, this is what I have come up with:
#!/usr/bin/perl
use strict;
use warnings;
my %data;
while (<DATA>) {
chomp;
if (s/^(.+)\[(\d+)\]/$1/) {
$data{$1} = $2;
}
else {
$data{$_} = 0;
}
}
for ( keys %data ) {
if ( $data{$_} ) {
print "$_\[$data{$_}:0\]\n";
}
else {
print "$_\n";
}
}
__END__
abc/def/gh[0]
abc/def/gh[1]
abc/def/gh[2]
ab/ef/xx
abc/def/gh[3]
Without going into the minute details, it seems you want to
This is probably the most efficient way to achieve this. Sorting will group all lines together that belong together. And joining adjacent lines is a simple and straightforward operation. If you know that all recurrent entries start at [0] and are contiguous, then you joining adjacent lines is even more trivial.