Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 253
  • Last Modified:

Perl script

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
pehalwaan
Asked:
pehalwaan
  • 6
  • 3
  • 3
  • +1
2 Solutions
 
tel2Commented:
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
 
pehalwaanAuthor Commented:
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
 
FishMongerCommented:
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
pehalwaanAuthor Commented:
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
 
FishMongerCommented:
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
 
pehalwaanAuthor Commented:
Thanks Fishmonger, Can you write a production script for me  with the approach you are suggesting.
0
 
FishMongerCommented:
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
 
pehalwaanAuthor Commented:
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
 
pehalwaanAuthor Commented:
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
 
ozoCommented:
for( @infile ){
                 s/(members.*)/$1,newhost.dl1.da/;
                print OUTFILE;
}
0
 
ozoCommented:
#!/usr/bin/perl

use strict;
use warnings;

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

while( <> ){
     chomp;
     $_ .= ',newhost.dl1.da'  if /members/;
     print;
}
0
 
pehalwaanAuthor Commented:
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
 
ozoCommented:
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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 6
  • 3
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now