#!/usr/local/bin/perl5 $label = "label"; $instance_flag = 0; while (<>) { if (/\(INSTANCE\s+(\w+)/) { if ($instance_flag == 0) { $instance = $1; $instance_flag = 1; } else { s/\//_/g; s/INSTANCE(\s+)(\w+)/INSTANCE${1}${instance}\/$2/; } } else { while (s/(\w+)\/(\w+)\//$1_$2\//) {} } s/(\w+)_(\W)/$1_${label}$2/; print; }