Solved

Perl script

Posted on 2012-04-12
13
244 Views
Last Modified: 2012-05-03
I have  below text in my file.


define hostgroup{
hostgroup_name   passive_disk-check-100-Mbps
alias            my_disk_check
members         sql05.dl1.da,fs01.dl1.da,iis02.dl1.da,mx04.dl1.da,mx04.chemi.dc1.da,mx03.chemi.cl.da
}

What i want to do is add a new host to the members line, so my addscript should adda host to the members line and it should look like below

define hostgroup{
hostgroup_name   passive_disk-check-100-Mbps
alias            my_disk_check
members       sql05.dl1.da,fs01.dl1.da,iis02.dl1.da,mx04.dl1.da,mx04.chemi.dc1.da,mx03.chemi.cl.da,newhost.dl1.da
}

And my delscript should remove a host from the  members line.
Can anyone help me in writing the code for the script.
0
Comment
Question by:pehalwaan
  • 6
  • 3
  • 3
  • +1
13 Comments
 
LVL 12

Assisted Solution

by:tel2
tel2 earned 250 total points
ID: 37840899
Hi pehalwaan,

Assuming this is in UNIX/LINUX, you could put something like this in a shell script:

To add a host:
    export ADDHOST=newhost.dl1.da
    perl -i -pe 's/^(members.*)$/$1,$ENV{ADDHOST}/' my_file

To delete a host:
    export DELHOST=newhost.dl1.da
    perl -i -pe 's/^(members.*?)($ENV{DELHOST},|,$ENV{DELHOST})(.*)$/$1$3/;s/,,/,/' my_file

Notes:
- Perl's "-i" switch modifies the file in place (i.e. overwrites it).  Use something like "-i.bak" to rename the old file to my_file.bak.
- If the above don't work, you may have to split the exports into 2 commands, i.e.:
  ADDHOST=newhost.dl1.da
  export ADDHOST

Or do you want a pure Perl solution (addscript.pl & delscript.pl)?
What is the OS?
0
 
LVL 3

Author Comment

by:pehalwaan
ID: 37841842
Thanks for the shell solution. I have the shell script solution. I want a pure perl solution(not the perl oneliners). I am using Linux platform.
0
 
LVL 28

Expert Comment

by:FishMonger
ID: 37842689
What have you tried thus far?

Is that the complete contents of your text file, or does it have multiple "hostgroup" blocks?

Is there additional text in the file other than the "hostgroup" block(s)?

You could use either the flip-flop operator or reset the input record separator to read the file in chunks/blocks and then process the data.
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 3

Author Comment

by:pehalwaan
ID: 37842701
Yes, it's my complete text file. There are no multiple hostgroups blocks in the file or any other additional text in the file.
0
 
LVL 28

Expert Comment

by:FishMonger
ID: 37842831
Here is one approach, there are several others.  Also, this is just a simple proof of concept and is not meant to be used in a production environment without additional checks.
use strict;
use warnings;

while (my $line = <DATA>) {
    chomp $line;
    if ( $line =~ /members/ ) {
        $line .= ',newhost.dl1.da';
    }
    print $line, "\n";
}

__DATA__
define hostgroup{
hostgroup_name   passive_disk-check-100-Mbps
alias            my_disk_check
members         sql05.dl1.da,fs01.dl1.da,iis02.dl1.da,mx04.dl1.da,mx04.chemi.dc1.da,mx03.chemi.cl.da
}

Open in new window

In an actual production script I'd use Getopt::Long to handle command line args that specify the member and if it is to be added or deleted.  I'd then have a subroutine for each and I'd use a more complex data structure.
0
 
LVL 3

Author Comment

by:pehalwaan
ID: 37842857
Thanks Fishmonger, Can you write a production script for me  with the approach you are suggesting.
0
 
LVL 28

Expert Comment

by:FishMonger
ID: 37842954
At the moment, I'm tied up on a project of my own and don't have the time to write a production level script for you, but if you give it a try and post back with your code when you get stuck, I'll see what I can do to help.

Start by reading the documentation for the Getopt::Long module and write a detailed outline of what the script should do.
0
 
LVL 3

Author Comment

by:pehalwaan
ID: 37843050
I will post the code but in the meantime if anyone else can write the  script , that would be really appreciable. I am in a hurry for this script.
0
 
LVL 3

Author Comment

by:pehalwaan
ID: 37843192
Below is my sample code which i am thinking of right now.
#!/usr/bin/perl

use strict;
use warnings;

#my $host=shift;
my @infile;
my $file="/tmp/test2";

        unless(open(INFILE, "$file")) {
                print " Unable to open $file for reading";
        }


        @infile=<INFILE>;
        close INFILE;

        unless(open(OUTFILE, "> $file")) {
                print"  Unable to truncate $file for writing";
        }

        while(my $line = <@infile>) {
                 if ( $line =~ /members/ ) {
                 $line .= ',newhost.dl1.da';
                        }
                print OUTFILE;
        }
close OUTFILE;

Open in new window


But it is giving  below errors-


Use of uninitialized value $_ in print at roledelete1 line 26.
Use of uninitialized value $_ in print at roledelete1 line 26.
Use of uninitialized value $_ in print at roledelete1 line 26.
Use of uninitialized value $_ in print at roledelete1 line 26.
Use of uninitialized value $_ in print at roledelete1 line 26.
Use of uninitialized value $_ in print at roledelete1 line 26.
Use of uninitialized value $_ in print at roledelete1 line 26.
Use of uninitialized value $_ in print at roledelete1 line 26.
Use of uninitialized value $_ in print at roledelete1 line 26.

FishMonger, Can you look into it.
0
 
LVL 84

Expert Comment

by:ozo
ID: 37843271
for( @infile ){
                 s/(members.*)/$1,newhost.dl1.da/;
                print OUTFILE;
}
0
 
LVL 84

Accepted Solution

by:
ozo earned 250 total points
ID: 37843335
#!/usr/bin/perl

use strict;
use warnings;

local @ARGV=("/tmp/test2");
$^I=".old";
$\=$/;

while( <> ){
     chomp;
     $_ .= ',newhost.dl1.da'  if /members/;
     print;
}
0
 
LVL 3

Author Comment

by:pehalwaan
ID: 37843520
Thanks ozo. One more help. Can you explain me the functioning of below two commands

ADD-

s/(members.*)/$1,newhost.dl1.da/;

DELETE-

s/^(members.*?)($host,|,$host)(.*)$/$1$3/;s/,,/,/

Thanks Once again.
0
 
LVL 84

Expert Comment

by:ozo
ID: 37843601
perl -MYAPE::Regex::Explain -e '$host="interpolate scalar host";print YAPE::Regex::Explain->new(qr/^(members.*?)($host,|,$host)(.*)$/)->explain'
The regular expression:

(?-imsx:^(members.*?)(interpolate scalar host,|,interpolate scalar host)(.*)$)

matches as follows:
 
NODE                     EXPLANATION
----------------------------------------------------------------------
(?-imsx:                 group, but do not capture (case-sensitive)
                         (with ^ and $ matching normally) (with . not
                         matching \n) (matching whitespace and #
                         normally):
----------------------------------------------------------------------
  ^                        the beginning of the string
----------------------------------------------------------------------
  (                        group and capture to \1:
----------------------------------------------------------------------
    members                  'members'
----------------------------------------------------------------------
    .*?                      any character except \n (0 or more times
                             (matching the least amount possible))
----------------------------------------------------------------------
  )                        end of \1
----------------------------------------------------------------------
  (                        group and capture to \2:
----------------------------------------------------------------------
    interpolate scalar       'interpolate scalar host,'
    host,
----------------------------------------------------------------------
   |                        OR
----------------------------------------------------------------------
    ,interpolate             ',interpolate scalar host'
    scalar host
----------------------------------------------------------------------
  )                        end of \2
----------------------------------------------------------------------
  (                        group and capture to \3:
----------------------------------------------------------------------
    .*                       any character except \n (0 or more times
                             (matching the most amount possible))
----------------------------------------------------------------------
  )                        end of \3
----------------------------------------------------------------------
  $                        before an optional \n, and the end of the
                           string
----------------------------------------------------------------------
)                        end of grouping
----------------------------------------------------------------------



perl -MYAPE::Regex::Explain -e '$host="interpolate scalar host";print YAPE::Regex::Explain->new(qr/,,/)->explain'
The regular expression:

(?-imsx:,,)

matches as follows:
 
NODE                     EXPLANATION
----------------------------------------------------------------------
(?-imsx:                 group, but do not capture (case-sensitive)
                         (with ^ and $ matching normally) (with . not
                         matching \n) (matching whitespace and #
                         normally):
----------------------------------------------------------------------
  ,,                       ',,'
----------------------------------------------------------------------
)                        end of grouping
----------------------------------------------------------------------
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question