0

I would like to compare the 2nd column of file1- freememory with the 2nd column-usedmemory of file2. If file1(2nd column) that is "freememory" > file2 (2nd column) that is "usedmemory" then print file2(1st column)-"machine" can be relocated to file1(1st column)-"storage", else file2(1st column)-"machine" CANNOT be relocated to file1(1st column)-"storage",

The comparison should be between 1st line of file2 and 1st line of file1. 2nd line of file2 and 2nd line of file1. Which means nth line of file2 should be compared with nth line of file1 only.

Both the files are sorted in descending order.

file1 is sorted based on descending order of column2
file2 is sorted based on descending order of column2.

file1-

Storage,Freememory  

0843,282856  
0867,270891   
0842,232803   
0868,213426  
0849,188785  
0844,188784  
0860,169249  
0855,169246  
0862,169245  
0853,169244  
0850,112497  
0857,112496  
0841,112496  
0839,112495  
0848,112494  
0851,112493   

file2 -

Machine,UsedMemory  
x0aaa06,111113232    
x0aaa05,78851    
x0aaa01,10243    
x0aaa03,4099    

Desired output -

x0aaa06  cannot be relocated to 0843  
x0aaa05 can be relocated to 0867  
x0aaa01 can be relocated to 0842  
x0aaa03 can be relocated to 0868  

1 Answer 1

1

I'll assume file1 doesn't have an empty 2nd line.

paste -d, file1 file2 | awk -F, 'NR>1{if ($2 > $4) print $3,"can be relocated to",$1 ; else print $3,"cannot be relocated to",$1}'

Using paste to feed awk a single "file" consisting of the combined columns of the respective lines.

The awk itself is pretty straightforward, starting at line 2 (NR > 1) and using print instead of printf because I'm lazy.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .