LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Perl Programming help (https://www.linuxquestions.org/questions/programming-9/perl-programming-help-715474/)

tuxtutorials 03-30-2009 06:54 AM

Perl Programming help
 
Hello, linuxquestion users. I have the following Perl problem I have been banging away at and have been stuck with for a long time. The issue is with a some basic search and replacement within a file.

Here is the code I got so far. First I tried opening the file then I slurped it in for experimentation. My goal was to patter match "flushing-" store it into an array then loop through the file and replace "Windows" in Windows-service with the stored "location" number.

Code:

$IN = "$INFILE";
$filename = "services.cfg";


#open ($IN, "$filename") || die "Error reading file $!";

#while (<$IN>) {
        #if (/(Windows-/) {
        #$match = "$1";
        #}

open my $file_handle, 'services.cfg' || die "file can't be slurped:\n$!";

local $/;  # Set input to "slurp" mode.
my @string = <$file_handle>;


foreach (@string) {
if (@string =~ /(Windows-)/) {
$match = "$1";
}

print $match;

}



close $file_handle;


Here is what my data looks like originally:
Code:



define service {
        service_description            Win_Term-Service_Check
        use                            Windows-Service
        host_name                      kentucky-srvboxen2
        check_command                  check_win_trm_svcs
}


define service {
        service_description            Check_NRPE_disk_D:
        use                            Windows-Service
        host_name                      alabama-srv0022
        check_command                  Check_NRPE_disk_D:
}


define service {
        service_description            Check_NRPE_Win_CPULOAD
        use                            Windows-Service
        host_name                      flushing-srv001
        check_command                  Check_NRPE_Win_CPULOAD
}


This is the service file for or Nagios.cfg. I basically need to strip out the location such as flushing, alabama, kentucky and replace the "Windows-service" with "location-service." My final Services.cfg file with look like so:

Code:

define service {
        service_description            Check_NRPE_Win_CPULOAD
        use                            flushing-Service
        host_name                      flushing-srv001
        check_command                  Check_NRPE_Win_CPULOAD
}

Is this best done in Perl or should other utilities such as sed be used?

Thanks for all the help

Sergei Steshenko 03-30-2009 07:12 AM

There is too much confusion in your code. For example:

Code:

local $/;  # Set input to "slurp" mode.
my @string = <$file_handle>;

Reread info on $/ and on how data from file handles is read.

And in Perl you can do almost anything and much more than in sed/awk.

And this:

Code:

if (@string =~ /(Windows-)/)
is also wrong - reread info on matching, regular expressions, replacement, array vs scalar context.

ghostdog74 03-30-2009 08:47 AM

awk
Code:

awk '
/use/{
  getline line
  m=split(line,a," ")
  n=split(a[2],b,"-")
  gsub("Windows",b[1])
  print ; print line;next
}1'  config.cfg


Sergei Steshenko 03-30-2009 09:53 AM

Quote:

Originally Posted by ghostdog74 (Post 3492514)
awk
Code:

awk '
/use/{
  getline line
  m=split(line,a," ")
  n=split(a[2],b,"-")
  gsub("Windows",b[1])
  print ; print line;next
}1'  config.cfg


And if the/an OP makes mistakes in awk code, you'll give him/her a working solution in Python/Perl :-) ?

I'm talking about giving fish or teaching to fish.

tuxtutorials 03-30-2009 10:18 AM

Thanks for the help guys let me stumble around with it some more and I will try to post back with a updated solution.

Telemachos 03-30-2009 11:29 AM

I will give you one tip. I don't think you really want slurp mode. That is, you don't want to save the entire file to a single scalar. That would make it very unwieldy to work with. Consider reading by paragraphs and then splitting on newlines, as a contrast:
Code:

#!/usr/bin/env perl
use strict;
use warnings;

open my $fh, 'services.cfg' or die "Can't open 'services.cfg': $!";

my @records;

{
  local $/ = "";    # Set for paragraph reading
  while (<$fh>) {
    my @records = split /\n/, $_;
    # Insert code here to get the place name from $record[3]
    # Insert code here to put the item found from $record[3] into $record[2]
    foreach my $line (@records) {
      print "$line\n";
    }
    print "\n";
  }
}

This gives output like the following:
Code:

hektor ~/practice $ perl fixer
define service {
        service_description            Win_Term-Service_Check
        use                            Windows-Service
        host_name                      kentucky-srvboxen2
        check_command                  check_win_trm_svcs
}

define service {
        service_description            Check_NRPE_disk_D:
        use                            Windows-Service
        host_name                      alabama-srv0022
        check_command                  Check_NRPE_disk_D:
}

define service {
        service_description            Check_NRPE_Win_CPULOAD
        use                            Windows-Service
        host_name                      flushing-srv001
        check_command                  Check_NRPE_Win_CPULOAD
}

Since you know the structure of the records this well, you can work on the two items you care about easily now. You know that the line with the name you need is in $record[3] and that the one you want to edit is in $record[2].

Telemachos 03-30-2009 12:40 PM

Double post. Sorry.

ghostdog74 03-30-2009 07:22 PM

Quote:

Originally Posted by Sergei Steshenko (Post 3492579)
I'm talking about giving fish or teaching to fish.

well, can i trust you to give this advice to any other forummers who post answers in full code next time? :)
Oh, also i would also expect you to be a role model and only give hints to answers next time for every post you make? :)


All times are GMT -5. The time now is 07:05 PM.