1
votes

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.

2
What have you tried and what problems are you running into? And why is this tagged Python?kindall
Hello.. I was mostly trying with awk. I tagged Python to see if there is a quick solution for this using Python.user3275818
Well, it'd be pretty straightforward to do in Python, but regardless, you'll have to post some code before we can tell you what's wrong with it!kindall
I am new in the field. So I was trying to solve the first part using awk. awk '($1 != $2)' print$0 file. I am sorry for the bad code. But i am a beginner.user3275818

2 Answers

1
votes

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.
0
votes

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