Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

How to replace string in file2 that matches first column of file1 with second column of file1

by anakin30 (Acolyte)
on Apr 25, 2013 at 04:32 UTC ( #1030576=perlquestion: print w/ replies, xml ) Need Help??
anakin30 has asked for the wisdom of the Perl Monks concerning the following question:

I'm still beginner in perl programming. I will appreciate is someone can provide advice or some clue to create perl script below

The file1 is comma separated file(csv).
Read every line from file1
I need to replace string(name) in file2 that matches first column of file1 with second column of file1

############################################### file 2 ------ DN: CN=Itmanagement,OU=Groups,O=uni.com ChangeType: Modify Add: uniquemember uniquemember: uid=anakin,OU=Users,o=uni.com DN: CN=ComputerScience,OU=Groups,o=uni.com ChangeType: Modify Add: uniquemember uniquemember: uid=obiwan,OU=Users,o=uni.com DN: CN=accoutning,OU=Groups,o=uni.com ChangeType: Modify Add: uniquemember uniquemember: uid=skywalker,OU=Users,o=uni.com DN: CN=management,OU=Groups,o=uni.com ChangeType: Modify Add: uniquemember uniquemember: uid=anakin,OU=Users,o=uni.com ###############################################
file 1 ------ anakin,james obiwan,nicol skywalker,sarah darthvader,robert

Comment on How to replace string in file2 that matches first column of file1 with second column of file1
Select or Download Code
Re: How to replace string in file2 that matches first column of file1 with second column of file1
by hdb (Prior) on Apr 25, 2013 at 05:28 UTC

    As a general outline:

    • Read file 1 line by line and chomp;
    • Split on comma my ($last, $first) = split /,/;
    • and store in hash: $name{$last} = $first;
    • Read file 2 line by line
    • Match against /uid=(?:.*),/, non-greedily
    • Replace $1 with $name{$1} if it exists.

Re: How to replace string in file2 that matches first column of file1 with second column of file1
by 2teez (Priest) on Apr 25, 2013 at 05:48 UTC

    Something like this:

    use warnings; use strict; use Inline::Files; my %name_change; while (<FILE_1>) { chomp; next if /^$/; my ( $k, $v ) = split /,/, $_; $name_change{$k} = $v; } while (<FILE_2>) { chomp; s/(uid=)(.+?),/$1$name_change{$2},/ if /uid/; print $_, $/; } __FILE_1__ anakin,james obiwan,nicol skywalker,sarah darthvader,robert __FILE_2__ DN: CN=Itmanagement,OU=Groups,O=uni.com ChangeType: Modify Add: uniquemember uniquemember: uid=anakin,OU=Users,o=uni.com DN: CN=ComputerScience,OU=Groups,o=uni.com ChangeType: Modify Add: uniquemember uniquemember: uid=obiwan,OU=Users,o=uni.com DN: CN=accoutning,OU=Groups,o=uni.com ChangeType: Modify Add: uniquemember uniquemember: uid=skywalker,OU=Users,o=uni.com DN: CN=management,OU=Groups,o=uni.com ChangeType: Modify Add: uniquemember uniquemember: uid=anakin,OU=Users,o=uni.com
    Output:
    DN: CN=Itmanagement,OU=Groups,O=uni.com ChangeType: Modify Add: uniquemember uniquemember: uid=james,OU=Users,o=uni.com DN: CN=ComputerScience,OU=Groups,o=uni.com ChangeType: Modify Add: uniquemember uniquemember: uid=nicol,OU=Users,o=uni.com DN: CN=accoutning,OU=Groups,o=uni.com ChangeType: Modify Add: uniquemember uniquemember: uid=sarah,OU=Users,o=uni.com DN: CN=management,OU=Groups,o=uni.com ChangeType: Modify Add: uniquemember uniquemember: uid=james,OU=Users,o=uni.com

    If you tell me, I'll forget.
    If you show me, I'll remember.
    if you involve me, I'll understand.
    --- Author unknown to me

      "If you tell me, I'll forget."

        And "If you show me, I'll remember."
        I simply shown the OP, a blue-print of what to do, he/she would have to build the building to completion.
        Nice one Not a Number :)

        If you tell me, I'll forget.
        If you show me, I'll remember.
        if you involve me, I'll understand.
        --- Author unknown to me

      Thank you so much for the reply.
      I will go through the script to understand more better
      Thanks once again

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (12)
As of 2014-12-18 18:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (59 votes), past polls