I have 3 CSV files which I am using as input. They do not necessarily have identical server names nor are they necessarily in the same sort order. Also a particular input file may have more than 1 line with the same server name (see Server wsomddvfxa01 in File A.
File A Server,Avg CPU,P95 CPU,Avg Mem Util,P95 Mem Util WSOMQAVPRA05,93.75,95.87,66.67,68.13 wsomdavpra03,90.39,94,65.77,68.51 wsomddvfxa01,39.22,92.19,82.59,88.25 wsomddvfxa01,35.45,89.23,79.89,83.24 File B Server,Avg CPU,P95 CPU,Avg Mem Util,P95 Mem Util WSOMQAVPRA05,34.78,100,55.1,67.6 wsomdavpra03,69.04,98.55,84.07,89.73 wsomddvfxa01,92.44,97.54,67.72,71.69 wsompapgtw05,48.77,96.9,92.1,93.55 File C Server,Avg CPU,P95 CPU,Avg Mem Util,P95 Mem Util WSOMQAVPRA05,93.13,98.11,68.95,73.47 wsomdavpra03,68.85,97.56,76.35,98.23 wsomddvfxa01,46.97,96.29,88.23,94.02 wsompapgtw05,30.66,93.74,39.89,71.35

What I am trying to do is for each Server (in Column 1) I want to get the Avg CPU (in Column 2) from each of the 3 files. In the files above this would produce

File OUT WSOMQAVPRA05,93.75,34.78,93.13 wsomdavpra03,90.39,69.04,68.85 wsomddvfxa01,39.22,92.44,46.97 wsompapgtw05,0,48.77,30.66

Notice in File OUT for Server Name (wsompapgtw05), since wsompapgtw05 does not appear in File A, the value is replaced with '0'. I was able to do this in my perl code (i.e., place a '0' in the output file when a particular server name appears in at least 1 of the input files, but not in all of the input files).

I have not yet been able to accomplish the 2nd desired task. Notice in File OUT the values for Server Name (wsomddvfxa01) are taken from the first line in File A. I want my perl code to pick out the Avg CPU value from the first data line when there are more than one line for a given Server Name in one of the input CSV data files.

My perl code looks as follows:

#!/usr/bin/perl use strict; use warnings; ################################################ # # Create File with Avg CPU Numbers for each Server # Name # ############################################### my %usage; my $files = 0; for my $file (”sfull1ns.dat”,”sfull2ns.dat”,”sfull3ns.dat”) { open (my $fh,”<”,$file) or die “Can’t open file $file: $!”; while (my $line = <$fh>) { chomp($line); my ($server, @data) = (split(“,”,$line)); if ($data[0] lt “!” ) { $data[0] = 0; } next if grep /[^0-9.]/, @data; $usage{$server} = [] unless exists $usage{$server}; push @{$usage{$server}}, 0 while @{$usage{$server}} + < $files; push @{$usage{$server}}, $data[0]; } continue { $files++ if eof; } close $fh or die “Can’t close file $file: $!”;

Code tags added by GrandFather


In reply to Eliminating Duplicate Lines From A CSV File by country1

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.