Reputation: 11
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
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
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
Upvotes: 1