in reply to Re: How to parse a text file
in thread How to parse a text file
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....
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: How to parse a text file
by jethro (Monsignor) on Aug 10, 2009 at 16:47 UTC | |
by xoroz (Initiate) on Aug 11, 2009 at 10:59 UTC | |
|
Re^3: How to parse a text file
by ww (Archbishop) on Aug 10, 2009 at 14:58 UTC |