Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

I have 2 .csv files as below:

# cat vm.csv vm_name,vm_cluster vm1,fd1 vm2,fd2 vm3,fd3 vm4,fd4

and

# cat vfiler_fd.csv vm_name,vFiler_IP,vFiler_cluster vm3,1.1.1.3,fd2 vm4,1.1.1.4,fd1 vm1,1.1.1.1,fd4 vm2,1.1.1.2,fd3

Goal:

Compare the 1st field of (vm_name) the two files and if they match then write it to a 3rd CSV file with the column headings as mentioned below (Each row of this 3rd CSV should conain all information about a given vm from both the CSVs files. Basically, I am trying to merge two files lokking at the 1st field.)

vm_name,vm_cluster,vFiler_IP,vFiler_cluster vm1,fd1,1.1.1.1,fd4 vm2,fd2,1.1.1.2,fd3 . . ....etc.

Here is my code which not correct But you will get the idea what I am trying to do:

use strict; use Text::CSV; use Class::CSV; my $report_csv = Class::CSV->new ( filename => "final.csv", fields => [qw/VM_Name VM_Cluster +vFiler_IP vFiler_Cluster MisMatch/], ); my $vm_mapping = Text::CSV->new ({ binary => 1}); my $vfiler_mapping = Text::CSV->new ({ binary => 1 }); open my $vm, "<:encoding(utf8)", "vm.csv" ; open my $vfiler, "<:encoding(utf8)", "vfiler_fd.csv"; while (my $vm_row = $vm_mapping->getline ($vm)) { while (my $vfiler_row = $vfiler_mapping->getline ($vfiler) ) { if ( $vm_row->[0] eq $vfiler_row->[0] ) $report_csv->add_line( { VM_Name => $vm_ro +w->[0], VM_Cluster => $vm_ro +w->[1], vFiler_IP => $vfile +r_row->[1], vFiler_Cluster=> $vfile +r_row->[2], MisMatch => "huhuh +huhuhu", } ); next; } next; }

The logic I used:

retrieve the 1st element of the vm.csv and comapre it with the 1st elements of all the rows of the vFiler.csv. if there is match, then write a new line whihc contains repsective elememnts to a new file called new.csv.

Issues:

this is not working.

I think nested while loop is a horrible practice. anyway, it is not working either.

I am not able to find a way to write "Column_names" with Text::CSV hence using Class:CSV and still it is not working.

I am pretty sure there is a better apporach to do this. Could you help. thanks.


In reply to Compare 2 CSV files and create a new CSV file out of comparision by slayedbylucifer

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2024-04-25 16:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found