Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9316743
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T02:41:59+00:00 2026-06-19T02:41:59+00:00

I am using perl and want to read an external file with the following

  • 0

I am using perl and want to read an external file with the following structure (full code at the end of the post):

servicestatus {
  parameter1 = abc
  parameter2 = abc
  parameter3 = abc
  }

servicecomment {
  parameter1 = def
  parameter2 = abc
  parameter3 = ghi
  }

those blocks are located in different parts of the file, the status block is in the upper while the comment-block is in the lower part. (each commentblock is related to a certain statusblock)

now i’m trying to scan the file line-by-line, so i’m encountering a statusblock at first, read a certain parameter (i.e. parameter2) and finally search the rest of the file for a commentblock that has an equal parameter.

the script i have written to solve this problem is extremely nested, in addition to that it only finds the first statusblock, its corresponding commentblock and then leaves the loop, although there is much more to find. any help considering this problem is welcomed!

Here is the full code

while ($line = <SF>) {
  if ($line =~ /servicestatus/) {
    while ($line = <SF>) {
      if ($line =~ /service_description/) {
        $service_desc = $line;
        while ($line = <SF>) {
          if ($line =~ /servicecomment/) {
            while ($line = <SF>) {
              if ($line =~ /service_description/) {
                $service_desc2 = $line;
                if ($service_desc eq $service_desc2) {
                  while ($line = <SF>) {
                    if ($line =~ /comment_id/) {
                      if ($line =~ m/(\d+)/) {
                        $comment_id = $1;
                        while ($line = <SF>) {
                          if ($line =~ /entry_time/) {
                            if ($line =~ m/(\d+)/) {
                              $entry_time = $1;
                              if ($entry_time < $time_oldest) {

                                # Kommando wird in die Commandpipe geladen
                                open(CP, ">>$o_commandpipe") or die("nagios.cmd not found");

                                # Befehl wird ausgegeben, External Command DEL_SVC_COMMENT wird ausgeführt
                                print {CP} "[$time_now] DEL_SVC_COMMENT;$comment_id;$time_now\n";
                                close(CP);
                                print("Comment $comment_id deleted.\n");
                              }
                            }
                          }
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

close SF;

for a better understanding: this script is for nagios (server monitoring tool) and should delete user-defined comments for certain services after a particular time.

thanks in advance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-19T02:42:00+00:00Added an answer on June 19, 2026 at 2:42 am

    A couple of suggestions:

    • Nested loops are not an appropriate design for your problem. What you need is one loop that goes through the file, with logic inside it.
    • Will the comment block with the equal parameter always be after its corresponding service status block? If not, you also need to rethink the design of going through the file line by line.
    • Are the blocks always separated by blank lines, as in the example? If so, you could set $/ = "\n\n";. Then you could read the file block by block instead of line by line.

    Here is an example of how you could do this, using a hash to keep track of the ones you want to delete. I use the assumption that all blocks are separated by blank lines, but if that is not true, you could easily adapt this to ikegami’s method of going through the file.

    use warnings;
    use strict;
    
    {
        my %delete_params;
        local $/ = "\n\n";
        while (<DATA>)
        {
            if (/^servicestatus/ and /parameter2 = (.*)/)
            {
                $delete_params{$1}++; #Delete comments with this parameter.
            }
            elsif (/^servicecomment/ and /parameter2 = (.*)/ and 
                   exists $delete_params{$1})
            {
                next;   #Comment matched; do not print.
            }
    
            print $_;
        }   
    }
    
    __DATA__
    servicestatus {
      parameter1 = abc
      parameter2 = abc
      parameter3 = abc
      }
    
    servicestatus {
      parameter1 = abc
      parameter2 = xyz
      parameter3 = abc
      }
    
    servicecomment {
      parameter1 = def
      parameter2 = abc
      parameter3 = ghi
      }
    
    servicecomment {
      parameter1 = def
      parameter2 = xyz
      parameter3 = ghi
      }
    
    servicecomment {
      parameter1 = def
      parameter2 = rrr
      parameter3 = ghi
      }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to read and write to an existing file using perl. can any
I want to profile my Perl code by using Devel::DProf . when I am
I have a file that I want to read in using the File::Slurp module
I am trying to user perl to read a text file online, using a
I have a file after parse it using following code with open ('cl1_vs_cl1.blast', 'rb')
I have a perl script read Excel file and parse using a perl module
I want to parse an XML file using Perl . I was able to
I want to process a txt file using perl. I have succeeded in using
I have a perl file containing some hash references (abc.pl) and i want to
I have a huge xlsx file (aboutn 127 MB) and want to read using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.