tags:

views:

126

answers:

2

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
+4  A: 

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]
Alan Haggai Alavi
+1  A: 

Without going into the minute details, it seems you want to

  1. Sort the lines (order is irrelevant, right?)
  2. Join adjacent lines if they can be represented as prefix[0:x] or possibly prefix[x:y] (where prefix matches [a-z]+)

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.

MSalters
you are right! but i have no idea how you achieve this by line joining operation? can you show me the commandline?
Depends on your programming language. After sorting you partition your lines into ranges with the same prefix (ie. one partition would be all lines starting with abc/def/gh). Then, for each partition, you determine which numbers are present. If those numbers form a contiguous sequence x-y, replace those lines by a single `prefix`[x:y] line.
MSalters