in reply to Replace an asterisk '*" with the content of array

but this will just replace the '*' and dump the array but not what I want. Can someone help me here please.

So what should it do instead?

Start with this program

#!/usr/bin/perl -- use strict; use warnings; my @finalnwlist = "192.169.32.0/255.255.255.0"; my @lines = "/file1 *(rw,sync,no_root_squash,no_subtree_check)"; foreach(@lines) { $_ =~ s/\*/@finalnwlist/g; print "$_\n"; }

Replies are listed 'Best First'.
Re^2: Replace an asterisk '*" with the content of array
by deep27ak (Novice) on Sep 18, 2015 at 10:41 UTC

    I cannot hardcode the @finalnwlist value as I said it will vary on different machine. This gets input from xml file.

    nor I can hardcode @lines with exports permission as you have done here.

    The script should check each line replace '*' with each of the subnet value and should do the same for all the subnets for all the shares

    The output should be like
    /file1 192.169.32.0/255.255.255.0(rw,sync,no_root_squash,no_subtree_ch +eck) /file1 192.169.72.0/255.255.255.0(rw,sync,no_root_squash,no_subtree_ch +eck) /file1 192.169.73.0/255.255.255.0(rw,sync,no_root_squash,no_subtree_ch +eck) /file2 192.169.32.0/255.255.255.0(ro,sync,no_root_squash,no_subtree_ch +eck) /file2 192.169.72.0/255.255.255.0(ro,sync,no_root_squash,no_subtree_ch +eck) /file3 192.169.73.0/255.255.255.0(ro,sync,no_root_squash,no_subtree_ch +eck) # shares for Tsp InstallServer file3 192.169.32.0/255.255.255.0(ro,sync,no_subtree_check) file3 192.169.72.0/255.255.255.0(ro,sync,no_subtree_check) file3 192.169.73.0/255.255.255.0(ro,sync,no_subtree_check)

      You need to identify the lines without * to let those pass through. Then loop over the network list but use a copy of the line in the substitution each time

      #!perl use strict; my @finalnwlist = qw( 192.169.32.0/255.255.255.0 192.169.72.0/255.255.255.0 192.169.73.0/255.255.255.0); replace_exports('exports.txt'); sub replace_exports { my $filename = shift; open IN,'<',$filename or die "Error opening $filename for read : $!"; my @lines = <IN>; close IN; open OUT, '>', 'chg_'.$filename or die "Error opening $filename for write : $!"; for (@lines) { chomp; if (m/\*/){ print "Change $_\n"; for my $nw (@finalnwlist){ my $copy = $_; $copy =~ s/\*/$nw/; print OUT $copy."\n"; } } else { print OUT $_."\n"; } } close OUT }
      poj

      I cannot hardcode the @finalnwlist value as I said it will vary on different machine. This gets input from xml file. nor I can hardcode @lines with exports permission as you have done here.

      You don't have to :)

      You're struck, and the key to getting unstuck

      Starting a new file, with minimal amount of code and minimal amount of hardcoded data like Re: Replace an asterisk '*" with the content of array

      Then simply explain what that program should do using as many words as you possibly can without copy/paste

      Please do this in the next minute and post what you get

        Well :) I gotta go so I'll leave you with my thinking :D

        So asterix line, is duplicated for each subnet, and in each duplicate the asterix is replaced with the current subnet

        Call me Teddy Bear ;)

        Thanks for looking into my script, I got it was I was looking for, the script was good with few mistakes but the main problem was that my array was treating the complete list of ip and subnets as 1 element so had to split them and everything worked out fine

        final script
        my @finalnwlist = split (/\n/, join (' ', @networklist, @brnwlist, @vn +wlist)); my @entries; sub update_exports { my $exports = 'exports'; open (EXPORTS,"<exports") ||die "Error opening exports file"; my @lines = <EXPORTS>; close (EXPORTS); open (TMP_EXP, ">> exports_tmp") || die "File not found"; foreach $a (@lines) { if ( $a =~ m/\*/) { logmsg "Generating unrestricted entry.."; my ($share, $permission) = split (/\*/, $a); for my $b (@finalnwlist) { @entries = join ('', $share, $b, $permission) +; print TMP_EXP @entries; } } else { print TMP_EXP $a; } } close(TMP_EXP); }