ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
Last edited by tuxtutorials; 03-30-2009 at 06:58 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].
Last edited by Telemachos; 03-30-2009 at 03:04 PM.
Reason: Fix typo
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?
Last edited by ghostdog74; 03-30-2009 at 07:25 PM.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.