while () { my %counts; s|(/\w+)|$1 . ++$counts{$1}|ge; print OUTPUT; }