user3275818
user3275818

Reputation: 11

Compare two fields of two files and print if they do not match

I have two files. If Column1 of File1 doesnot march Column1 of File2 then print the whole line of File1 in the output file. If Column1 of both Files Matches, and if the value of Column2 of File1 say "N" is greater than "N+10" or lesser than "N-10" than the value of Column2 of File2, only then print the whole line of File1.

File1:

C1  23
C1  24
C2  66
C3  88
C6  100 
C7  79
C20 200

File2:

C1  44
C1  35
C2  70 
C4  88
C6  92
C7  90
C9  80

Expected Output:

C1  23
C1  24
C3  88
C7  79
C20 200

I would appreciate your help in solving this. Thank you.

Upvotes: 1

Views: 747

Answers (2)

Tiago Lopo
Tiago Lopo

Reputation: 7959

Since you have just two columns I suggest using paste to merge them, which will make awk's logic a lot easier:

paste file1 file2 | awk '{ if($1 != $3){print $1,$2}else if($4 > ($2 + 10) || $4 < ($2 -10 )){print $1,$2} }'
C1 23
C1 24
C3 88
C7 79
C20 200

Upvotes: 0

jaypal singh
jaypal singh

Reputation: 77105

Using awk you can do:

awk '
NR==FNR { 
    lines[NR,"col1"] = $1
    lines[NR,"col2"] = $2
    lines[NR,"line"] = $0
    next
}
(lines[FNR,"col1"] != $1) {
        print lines[FNR,"line"]
        next
}
(lines[FNR,"col2"]+10 < $2 || lines[FNR,"col2"]-10 > $2) {
        print lines[FNR,"line"]
}' file1 file2
C1  23
C1  24
C3  88
C7  79
C20 200
  • We read the first file and create a multi-dimensional array using the line number and fields as the key and storing column1, column2 and lines appropriately.
  • When we iterate the second file, we keep the checks in place and print the line if it matches the check points.

Upvotes: 1

Related Questions