-3

I have two CSV files and would like to compare Value1 of file1 with value1 of Files column. If the values in File2 are greater than write the contents to File3.

the output can be in a single row for both values.

file 1:

Date          Min Temp    Max Temp.
01/01/2016    16.5         28.90
02/01/2016    15.5         31.20
03/01/2016    17.5         26.90

file 2:

Date          Min Temp    Max Temp.
01/02/2017    18.6         26.90
02/02/2017    12.5         34.20
03/02/2017    21.5         29.90

Output

Date          Min Temp    Max Temp.    Comment
01/02/2017    18.6                    Min. Temp is higher
03/02/2017    21.5                    Min. Temp is higher
02/02/2017                34.20       Max. Temp is higher
03/02/2017                29.90       Max. Temp is higher

Thanks in advance for you help.

  • 7
    Are you sure your files are correct (file1 with .../01/... and file2 with .../02/..., input files with 2016 output file with 2017?). What to do for dates only present in one of the files? – Philippos Mar 16 '17 at 7:05
  • How do you decide which values are left blank in the output? – lgeorget Mar 16 '17 at 9:09
  • All values which are less in file1 when compared to file2 needs to be ignored. – Ven Mar 17 '17 at 8:17
  • First clarify are the CSVs comma separated or something else, for no commas are to be seen in the above. – user218374 Mar 17 '17 at 8:26
  • the values are comma separated. – Ven Mar 17 '17 at 10:16
0
perl -F, -lane '
   push(@h, [@F]),next if @ARGV;
   print(qq[$_,Comments]),next if ++$FNR == 1;
   $F[1] > $h[$FNR][1] and push @A, join q[,], $F[0], $F[1],   q[], q[Min. Temp is higher];
   $F[2] > $h[$FNR][2] and push @B, join q[,], $F[0],   q[], $F[2], q[Max. Temp is higher];
   eof and print for @A, @B;
' file1.csv file2.csv > file3.csv

Explanation

Set the Input Field Separator as comma to split and store fields in the @F for each line. Store file1 in an LoL, aka, a List of Lists, @h = ( [Line1:$F[0],$F[1],$F[2]], [Line2:$F[0],$F[1],$F[2]], ... ).

$FNR holds the line counter of file2.

Then fill up the arrays @A @B based on the criteria given.

Note that an LoL is accessed like a matrix $h[n][m] => the n-th list is first grabbed from the LoL @h and then from this grabbed list it's m-th element is selected.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.