LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-30-2009, 06:54 AM   #1
tuxtutorials
Member
 
Registered: Dec 2008
Location: New York
Distribution: RedHat, Solaris
Posts: 68

Rep: Reputation: 16
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

Last edited by tuxtutorials; 03-30-2009 at 06:58 AM.
 
Old 03-30-2009, 07:12 AM   #2
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
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.

Last edited by Sergei Steshenko; 03-30-2009 at 07:17 AM.
 
Old 03-30-2009, 08:47 AM   #3
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
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
 
Old 03-30-2009, 09:53 AM   #4
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by ghostdog74 View Post
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.
 
Old 03-30-2009, 10:18 AM   #5
tuxtutorials
Member
 
Registered: Dec 2008
Location: New York
Distribution: RedHat, Solaris
Posts: 68

Original Poster
Rep: Reputation: 16
Thanks for the help guys let me stumble around with it some more and I will try to post back with a updated solution.
 
Old 03-30-2009, 11:29 AM   #6
Telemachos
Member
 
Registered: May 2007
Distribution: Debian
Posts: 754

Rep: Reputation: 60
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
 
Old 03-30-2009, 12:40 PM   #7
Telemachos
Member
 
Registered: May 2007
Distribution: Debian
Posts: 754

Rep: Reputation: 60
Double post. Sorry.

Last edited by Telemachos; 03-30-2009 at 12:42 PM.
 
Old 03-30-2009, 07:22 PM   #8
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by Sergei Steshenko View Post
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
inquiry to Perl monks about Perl as a first programming language mannclay Programming 53 03-22-2009 01:35 PM
Perl Programming! emailssent Programming 3 06-05-2005 02:25 AM
Programming in Perl mimf Programming 5 12-04-2003 04:58 AM
Perl programming AliceC Programming 4 05-17-2001 10:18 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:00 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration