Jethro, you are the man.
You saved my day, the concept of Finite-state machine is perfect!
I modified a little few things and the script now works.
living and learning.
thanks again brother
here is the final script:

#! /usr/local/bin/perl # must add service_description inside "define service{" only in defini +tions that do not have it #Version 6 my $state="outside"; my @servicelines; my $desc_flag; #is 1 if description found(already exists, if i +ts 0 then call parse and output sd) my $file = "<test.txt"; my $sd = ""; open (MYFILE, '>services2.cfg'); open (TEXT_FILE, $file) || die "Can't open $file for read: $!"; while ($line = <TEXT_FILE>) { 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 th +e string sub trim($) { my $string = shift; $string =~ s/^\s+//; $string =~ s/\s+$//; return $string; }

In reply to Re^4: How to parse a text file by xoroz
in thread How to parse a text file by xoroz

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.