更改值后如何添加带有自定义文本的新行

时间:2019-05-29 12:23:02

标签: perl

我正在使用Perl脚本来更改.cfg文件中的值。 由于我是Perl的新手,所以我试图解释我认为应该做的事情。

每次脚本识别更改(优先级)时,它还应在.cfg文件中的现有行下添加一个新行,该行名为COMMENT =“”

例如:

COMMENT="changed 01.01.19 by DG"
COMMENT="xx.xx.xx xx.xx.xx by DG"

代码:

for ($i=0;$i<$pop_entries;$i++)
    {
    if ( index ($pop[$i], $startpattern ) >= 0 )
      {
      if ( @popblock > 3 )
        {
        # print ("Complete: $popblock[0]: ");
        $popheader = $popblock[0];
        $popheader =~ s/POP\=\"\* \(//g;
        $popheader =~ s/\)\"//g;
        $sender="";
        $receiver="";
        $objclass="";
        $priority="";
        for ($j=0;$j<@popblock;$j++)
          {
          if ( index ($popblock[$j], "POP_SENDER=\"") >= 0 ) { $sender = $popblock[$j]; $sender =~ s/POP_SENDER=//g ; $sender =~ s/\"//g ; $sender =~ s/ //g; }
          if ( index ($popblock[$j], "POP_OBJ_CLASS=\"") >= 0 ) { $objclass = $popblock[$j]; $objclass =~ s/POP_OBJ_CLASS=//g ; $objclass =~ s/\"//g ; $objclass =~ s/ //g;}
          if ( index ($popblock[$j], "POP_RECEIVER=\"") >= 0 ) { $receiver = $popblock[$j]; $receiver =~ s/POP_RECEIVER=//g ; $receiver =~ s/\"//g ; $receiver =~ s/ //g; }
          if ( index ($popblock[$j], "PRIORITY=\"") >= 0 ) { $priority = $popblock[$j]; $priority =~ s/PRIORITY=//g ; $priority =~ s/\"//g ; $priority =~ s/ //g; $priopos = $j; }
          }
        # print ("$sender * $objclass * $receiver * $priority");
        $pattern=$sender."|".$objclass."|".$receiver."|";
        $newprio="NIL";
        current_time ();
        for ($z=0;$z<@prio;$z++)
          {
          if ( index ($prio[$z], $pattern) >= 0 ) { $newprio = substr($prio[$z], rindex($prio[$z], "|") + 1, length($prio[$z]) - rindex($prio[$z], "|")); }
          }
        if ( $newprio eq "NIL" )
          {
          print (LOGFILE "$timestamp_log  $popheader ($sender * $objclass * $receiver): No entry found!\n");
          }
        else
          {
          if ( $newprio == $priority )
            {
            print (LOGFILE "$timestamp_log  $popheader ($sender * $objclass * $receiver): Nothing to do ($priority=$newprio)\n");
            }
          else
            {
            $popblock[$priopos] = "  PRIORITY=\"".$newprio."\"";
            print (LOGFILE "$timestamp_log  $popheader ($sender * $objclass * $receiver): Priority changed from $priority to $newprio\n");
            }
          }
        }
      for ($j=0;$j<@popblock;$j++)
         {
         print ( NEWPOP "$popblock[$j]\n");
         }
      undef (@popblock);
      push (@popblock, $pop[$i]);
      }
    else
      {
      push (@popblock, $pop[$i]);
      }
    } # for ($i=0;$i<$pop_entries;$i++)
  for ($j=0;$j<@popblock;$j++)
    {
    print ( NEWPOP "$popblock[$j]\n");
    }

0 个答案:

没有答案