Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Compare two files and delete

Status
Not open for further replies.

rkdash

Technical User
Jul 15, 2004
14
US
Hello,

I have two files: File1 has two columns and File2 has four columns. They also have different no. of rows.

I want to match column-1 of file1 with column-1 of file2
and column-2 of file1 with column-2 of file2. Then I want to print the matched records of file2 or write to file3.

The problem is that the values of the respective columns in the two files are not exactly matched. But It will be fine if they match within certain error bounds (e.g., +/- 0.2).

Please help.
#-------------------------------------------
File1:

-124.107 49.3516
-124.103 49.349
-124.099 49.3463

File2:
-122.230 48.850 1.20 20.80
-124.950 48.210 1.20 1.00
-124.730 48.130 1.20 23.70
-121.930 48.090 1.20 1.00
-124.100 49.3563 1.20 1.00


File3:
-124.100 49.3563 1.20 1.00

#--------------------------------------------


Thanks,

 
Brute force method:
awk '
function abs(x){return x<0 ? -x : x}
NR==FNR{++c;a[NR]=$1;b[NR]=$2;next}
{ for(i=1;i<=c;++i)
if(abs($1-a)<=0.2 && abs($2-b)<=0.2){
print;next
} }
' File1 File2 > File3

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top