V-V
V-V

Reputation: 73

comparing files against original file with different number of rows

I have set of files, 1 answers file and few results files I need to compare each of the results file with answers, but the result files may not have the same number or rows.

answer file

q1, true
q2, false
q3, false
q4, true
q5, true

results 1

q1, true
q2, false
q3, true

table I would like to have

q1, 1
q2, 1
q3, 0
q4, 0
q5, 0

so if the data does match then its 1 else its 0 and if the row does not exist consider it as 0 again --- the same action should happen for all the files in the result folder.

What I tried so far :

awk -F "," '{print $0}' answer.csv | grep -f - result01.csv > me.csv

which prints the correct answers only

and is there a chance to do the same if the data for results look like the following?

results 1

data/q1, true
data/q2, false
data/q3, true

Upvotes: 1

Views: 105

Answers (2)

Chris Seymour
Chris Seymour

Reputation: 85775

Using awk to compare the results file r against the answer a file:

$ awk 'FNR==NR{a[$1]=$2;next}$1 in a{print $1,a[$1]==$2;next}{print $1,0}' r a
q1, 1
q2, 1
q3, 0
q4, 0
q5, 0

Upvotes: 2

dogbane
dogbane

Reputation: 274532

Take a look at the comm command which can be used to compare two sorted files line by line.

For example:

$ comm  -2  <(sort answerFile) <(sort resultFile)
        q1 true
        q2 false
q3 false
q4 true
q5 true

The first column contains lines unique to file1 and the second column contains lines common to both files.

Upvotes: 2

Related Questions