Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Replace an asterisk '*" with the content of array

by wee (Scribe)
on Sep 23, 2015 at 23:22 UTC ( [id://1142846]=note: print w/replies, xml ) Need Help??


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

This might not be exactly what you're after, but I wrote it how I'd approach things. and I added more comments than I normally would in order to make it clear what direction I was after and explain things. It appears to do what you want.

#!/usr/bin/perl use warnings; use strict; use FindBin qw($Bin); use Tie::File; # A list of IPs and subnet masks to substitute for '*' as we read thro +ugh a file of share info. If # you need this from a file, consider File::Slurp. my @ips = 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); # The path and name of the share file. my $sharefile = "$Bin/sharefile"; # Will hold the position in the @ips array as we progress through the +file looking for matches. my $last_index = '0'; # Holds the contents of the share file. my @contents; # This maps array elements to lines in the share file. tie(@contents, 'Tie::File', $sharefile) or die "Cannot tie $sharefile +: $!\n"; # Read each line in the file. If we see a '*' we replace it with an en +try in the @ips array. If a # replacement happens, we increment $last_index to point to the index +number of the next element in # the @ips array. If we matched the last element, we reset the counter + back to zero and start at # the beginning. for (my $i = 0; $i <= $#contents; $i++) { if ($contents[$i] =~ s/\*/$ips[$last_index]/) { if (++$last_index == @ips) { $last_index = '0'; } } } untie(@contents); print "Done editing $sharefile.\n";

That assumes you have the share info in a file called 'sharefile' in the same directory as the script. After running, the sharefile file looks like this:

/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) /file2 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/255.255.255.0(ro,sync,no_subtree_chec +k) file3 192.169.72.0/255.255.255.0/255.255.255.0(ro,sync,no_subtree_chec +k) file3 192.169.73.0/255.255.255.0/255.255.255.0(ro,sync,no_subtree_chec +k)

I think that's what you were after anyway...

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1142846]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (None)
    As of 2024-04-25 01:07 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found