#! /usr/local/bin/perl # must add service_description inside "define service{" only in definitions that do not have it #Version 6 my $state="outside"; my @servicelines; my $desc_flag; #is 1 if description found(already exists, if its 0 then call parse and output sd) my $file = "services2.cfg'); open (TEXT_FILE, $file) || die "Can't open $file for read: $!"; while ($line = ) { if ($state eq "outside") { if ($line=~/^\s*define service/) { $state="inside"; @servicelines=(); #reset array push(@servicelines,$line ); $desc_flag=0; } } elsif ($state eq "inside") { if ($line=~/^\s*}/) { if ($desc_flag eq 0){ foreach $l(@servicelines) { if ($l=~/^\s*use/) { $sd = removesem($l); #print $sd; push(@servicelines,$sd); } } } push(@servicelines, "}\n"); # go thru entire array and print to file foreach $i(@servicelines) { print $i; print MYFILE $i; } $state="outside"; } elsif ($line=~/^\s*service_description/) { push(@servicelines, $line); $desc_flag=1; } else { push(@servicelines, $line); } } } close (TEXT_FILE); close (MYFILE); sub removesem($) { my $use = shift; $use =~ s/use/service_description/; #print "test: " . $use . " \n"; if($use =~ m/sem/ ) { #remove sem $use =~s/sem_//; return $use; }else{ return $use; } } # Perl trim function to remove whitespace from the start and end of the string sub trim($) { my $string = shift; $string =~ s/^\s+//; $string =~ s/\s+$//; return $string; }