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.
LVL 3
pehalwaanAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Perl

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.