Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Help asked for creating a file with usernames

by bertderidder (Initiate)
on Feb 09, 2013 at 12:46 UTC ( #1017958=perlquestion: print w/ replies, xml ) Need Help??
bertderidder has asked for the wisdom of the Perl Monks concerning the following question:

Hi,
i've got a text file with over 100 usernames and ip-addresses that are wrong in my ldap, i need to add /32 to the ip-addresses.
I don't have much knowledge of perl or other programming languages.
Now i'm looking for a way to adjust the username & ip-address so i can run an ldapmodify to correct all the wrong ip-addresses.

I don't now how to do it with perl, can someone help me in the right direction?

source file: users.txt dn: uid=donald,nameSpace=mydomain.com,ou=users,o=sp radiusFramedAddress: 192.168.0.2 dn: uid=mario,nameSpace=mydomain.com,ou=users,o=sp radiusFramedAddress: 192.168.0.7 dn: uid=thijs,nameSpace=mydomain.com,ou=users,o=sp radiusFramedAddress: 192.168.11.15
i'd would like to create a target file with the syntax that's below.
target file: addme.txt dn: uid=donald,nameSpace=mydomain.com,ou=users,o=sp changeType: modify replace: radiusFramedAddress radiusFramedAddress: 192.168.0.2/32 dn: uid=mario,nameSpace=mydomain.com,ou=users,o=sp changeType: modify replace: radiusFramedAddress radiusFramedAddress: 192.168.0.7/32 dn: uid=thijs,nameSpace=mydomain.com,ou=users,o=sp changeType: modify replace: radiusFramedAddress radiusFramedAddress: 192.168.11.15/32
Thanks

Comment on Help asked for creating a file with usernames
Select or Download Code
Re: Help asked for creating a file with usernames
by jethro (Monsignor) on Feb 09, 2013 at 14:19 UTC

    Well, generally perlmonks isn't a code-writing service, but since this is rather trivial, here is the one-liner you could use:

    perl -pi'.orig' -e 's/^radiusFramedAddress:\s* ([\d\.]*)/changeType: m +odify\nreplace: radiusFramedAddress\nradiusFramedAddress: $1\/32/' us +ers.txt

    It uses a regular expression (regexp) to substitute every line beginning with 'radiusFr...' with the three lines ou want. $1 inserts the ip-adress captured in the pattern through the ()

    Just execute this on a command line (as one single line wihtout the '+' chars) and you are finished. The original file is replaced and changed, the old contents is in users.txt.orig. Tested it on linux. If you are on windows, it should work too, but I can't guarantee (but there shouldn't be anything catastrophic happening)

Re: Help asked for creating a file with usernames
by flexvault (Parson) on Feb 09, 2013 at 14:40 UTC

    Welcome bertderidder,

    Make sure you have a good backup of the file first, and then run the script on a different copy altogether. Better save...

    Regards...Ed

    "Well done is better than well said." - Benjamin Franklin

Re: Help asked for creating a file with usernames
by johngg (Abbot) on Feb 09, 2013 at 14:49 UTC

    A more long-winded approach would be to read and print the file line by line and, if we have found an address line, print the two new preceding lines and add the /32 at the end.

    $ perl -Mstrict -Mwarnings -e ' > open my $usersFH, q{<}, \ <<EOD or die open qq{open: < HEREDOC: $!\n +}; > dn: uid=donald,nameSpace=mydomain.com,ou=users,o=sp > radiusFramedAddress: 192.168.0.2 > > dn: uid=mario,nameSpace=mydomain.com,ou=users,o=sp > radiusFramedAddress: 192.168.0.7 > > dn: uid=thijs,nameSpace=mydomain.com,ou=users,o=sp > radiusFramedAddress: 192.168.11.15 > EOD > > while ( <$usersFH> ) > { > if ( m{^radiusFramedAddress:} ) > { > print > qq{changeType: modify\nreplace: radiusFramedAddress\n}; > s{$}{/32}; > } > print; > }' dn: uid=donald,nameSpace=mydomain.com,ou=users,o=sp changeType: modify replace: radiusFramedAddress radiusFramedAddress: 192.168.0.2/32 dn: uid=mario,nameSpace=mydomain.com,ou=users,o=sp changeType: modify replace: radiusFramedAddress radiusFramedAddress: 192.168.0.7/32 dn: uid=thijs,nameSpace=mydomain.com,ou=users,o=sp changeType: modify replace: radiusFramedAddress radiusFramedAddress: 192.168.11.15/32 $

    I hope this is helpful.

    Cheers,

    JohnGG

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1017958]
Approved by McDarren
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (11)
As of 2014-10-01 21:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    What is your favourite meta-syntactic variable name?














    Results (40 votes), past polls