-1

I need to compare two files on 2nd column and generate result as output.csv using awk.

file1.csv

5, 0,
5, 1,
5, 1,
4, 1,
4, 1,
4, 1,

file2.csv

5, 0,
5, 1,
5, 1,
4, 0,
4, 2,
4, 3,

output.csv

5, 0,
5, 1,
5, 1,
4, D,
4, D,
4, D,

closed as unclear what you're asking by Scott, Stephen Rauch, Jeff Schaller, sebasth, αғsнιη Oct 12 '17 at 23:17

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • You still haven't described the mechanism for translating the input to the output. – Jeff Schaller Oct 12 '17 at 20:17
1

Assuming the two files have the same number of rows and that the rows in the two files corresponds to each other in a pairwise fashion:

$ paste file1 file2 | awk -F', *' -vOFS="," '$2 != $4 { $2 = "D" } { print $1, $2 }'
5,0
5,1
5,1
4,D
4,D
4,D

The paste command will put the two files side by side like so:

5, 0,   5, 0,
5, 1,   5, 1,
5, 1,   5, 1,
4, 1,   4, 0,
4, 1,   4, 2,
4, 1,   4, 3,

The awk code will then read this as data delimited by commas (followed by any number of spaces), and will, for the cases that the second column is not equal to the fourth column, set the second column to the character D. The code then prints out the first two column (the second possibly modified) with a comma as delimiter.

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