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":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.