I checked the Nagios::Object::Config; but I dont think its for me, since I just need to parse a .cfg file and I am not even in the nagios server...

If I just could find an element "{" inside the array mark its index find the end element "}" then split its values...

someone must have done simillar to these, I have searched for hours about arrays in perl but still no good...
my $use = ""; my $sd = ""; my $count = 1; my $bset= 0; # if new SD was set,change to 1, 0 means it was not se +t. my $bend = 0; # means definition is ended } was found 1, 0 means sti +ll needs to close definition. open (TEXT_FILE, "<test.txt"); open (MYFILE, '>services2.cfg'); while ($line = <TEXT_FILE>) { print MYFILE $line; print $count ." - " . $line; if ($line =~ m/use/) { $bset = 0; $bend = 0; print "-----Reseting bset and bend\n"; $host = <TEXT_FILE> ; print MYFILE $host; $nextline3 = <TEXT_FILE> ; $nextline4 = <TEXT_FILE> ; $nextline5 = <TEXT_FILE> ; $nextline6 = <TEXT_FILE> ; $use = $line; $use =~ s/use//; $use = trim($use); $host =~ s/host_name//; $host = trim($host); print $count . ". ", color("white") . $host ."\n", color("res +et"); # Should check if any of the lines already have service_description or + } #could use a loop foreach ($nextline(i))...i++ #Line 3 if ($nextline3 =~ m/}/ ) { #service_description does not exist and was not set, we are in the las +t line, we add SD and } and define service of next printf ("(L3=}) Adding SD and }\n"); $sd = sdparse($use); print color("yellow"), "\n-->NEW SD: ". $sd ."\n", color(" +reset"); print MYFILE " service_description ". $sd."\n}\n\n +define service{\n"; $bset = 1; $bend = 1; } if ($nextline3 =~ m/service_description/ && $bend eq 0) { print "(L3) SD here. \n"; print MYFILE $nextline3; $bset = 1; } if ($nextline3 !~ m/service_description/ && $bset eq 0 && $ben +d eq 0) { print MYFILE $nextline3; } if ($nextline4 =~ m/}/ && $bend eq 0 && $bset eq 0) { #service_description does not exist and was not set, we are in the las +t line, we add SD and } and define service of next printf ("(L4=}) Adding SD and }\n"); $sd = sdparse($use); print color("yellow"), "\n-->NEW SD: ". $sd ."\n", col +or("reset"); print MYFILE " service_description ". $sd."\n}\n +define service{\n\n"; $bset = 1; $bend = 1; } if ($nextline4 =~ m/service_description/ && $bset eq 0 && $ben +d eq 0) { print "(L4) SD here. \n"; print MYFILE $nextline4; $bset = 1; } if ($nextline4 !~ m/service_description/ && $bend eq 0 ) { if ($bset eq 0) { print "(L4) Is not SD,and SD not found so far keep goi +ng. \n"; print MYFILE $nextline4; } if ($bset eq 1) { print MYFILE $nextline4; } } #Line 5 etc....

In reply to Re^2: 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.