comparing a field with whole line of another file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting comparing a field with whole line of another file
# 1  
Old 10-06-2012
comparing a field with whole line of another file

Hello;
i have 2 file that each of them contain many records such as below:
file1
Code:
2006/01/01 12:36:49.3    37.902 48.482 0.0    1136119009
2006/01/01 13:16:22.9    34.933 46.292 2.0    1136121382
2006/01/01 13:54:33.2    37.762 48.163 18.8    1136123673
2006/01/01 14:32:9.1    37.739 48.330 23.2    1136125929
2006/01/01 21:14:32.4    36.986 46.063 18.0    1136150072

file2
Code:
2006/01/01 13:16:05.36 34.6370 46.8597 10.0    1136121379
2006/01/06 06:59:02.10 39.4099 44.6065 10.0    1136530742
2006/01/06 13:49:52.70 38.5481 40.7437 6.0      1136555392
2006/01/13 04:57:18.04 38.2197 47.8743 10.1    1137128238
2006/01/16 09:18:05.17 34.7801 45.0857 10.0    1137403085

now i want to compare each records of file1 with the whole lines of file2 and print both line of file1 and file2 which has these circumstances:
1-1st field of file1 must be equal to 1st field of file2
2-abslute difference value between 6th field of both file must be less than 5
3-abslute difference value between 3th field of both file must be less than 1
4-abslute difference value between 4th field of both file must be less than 1
i tried
Code:
NR=FNR

but i can't put circumstances in proper palace.
so for this example the output will be:
file3
Code:
 2006/01/01 13:16:22.9    34.933 46.292 2.0    1136121382  2006/01/01 13:16:05.36 34.6370 46.8597 10.0    1136121379


Last edited by saeed.soltani; 10-06-2012 at 04:02 PM..
# 2  
Old 10-06-2012
Try:
Code:
join -j1 file1 file2 | awk 'sqrt(($6-$11)^2)<5&&sqrt(($3-$8)^2)<1&&sqrt(($4-$9)^2)<1'

Note that it will not print column 1 from file 2...
This User Gave Thanks to bartus11 For This Post:
# 3  
Old 10-06-2012
Code:
awk 'FNR==NR{a[$1]=$0;next}{split(a[$1],M,FS);FV=((M[6]-$6) >=0) ? (M[6]-$6) : ($6-M[6]);
SV=((M[3]-$3) >=0) ? (M[3]-$3) : ($3-M[3]); TV=((M[4]-$4) >=0) ? (M[4]-$4) : ($4-M[4]);
if(FV < 5 && SV < 1 && TV < 1){print a[$1],$0}}' file2 file1

after looking at bartus11's solution..

Code:
awk 'FNR==NR{a[$1]=$0;next}{split(a[$1],M,FS);if(sqrt(($6-M[6])^2)<5&&sqrt(($3-M[3])^2)<1&&sqrt(($4-M[4])^2)<1){
{print a[$1],$0}}}' file2 file1

This User Gave Thanks to pamu For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Printing string from last field of the nth line of file to start (or end) of each line (awk I think)

My file (the output of an experiment) starts off looking like this, _____________________________________________________________ Subjects incorporated to date: 001 Data file started on machine PKSHS260-05CP ********************************************************************** Subject 1,... (9 Replies)
Discussion started by: samonl
9 Replies

2. Shell Programming and Scripting

Comparing two files field and position

I have two files : file1 and file2 file1 format: 7026-H70 7026-1017685 7026-H70 7026-1017687 7026-B80 7026-108D65A 7026-B80 7026-108D67A 7046-B50 7026-1034B4A File2 format : mt01cp01 7026-B80 01108D69A mt01cp02 7026-B80 01108D68A mt01sv01 7046-B50... (7 Replies)
Discussion started by: amir07
7 Replies

3. Shell Programming and Scripting

Command/script to match a field and print the next field of each line in a file.

Hello, I have a text file in the below format: Source Destination State Lag Status CQA02W2K12pl:D:\CAQA ... (10 Replies)
Discussion started by: pocodot
10 Replies

4. Shell Programming and Scripting

Awk: Comparing arguments with in line values of file and printing the result

I need to develop a script where I will take two date arguments as parameter date1 and date2 which will in format YYYYMM. Below is the input file say sample.txt. sample.txt will have certain blocks starting with P1. Each block will have a value 118,1:TIMESTAMP. I need to compare the... (7 Replies)
Discussion started by: garvit184
7 Replies

5. Shell Programming and Scripting

Reading text file, comparing a value in a line, and placing only part of the line in a variable?

I need some help. I would like to read in a text file. Take a variable such as ROW-D-01, compare it to what's in one line in the text file such as PROD/VM/ROW-D-01 and only input PROD/VM into a variable without the /ROW-D-01. Is this possible? any help is appreciated. (2 Replies)
Discussion started by: xChristopher
2 Replies

6. UNIX for Dummies Questions & Answers

Comparing to specific line in file bash script

Hi, I have a value stored in x and I need to compare it to the numbers in every other line of a file. The file contains alternating lines of numbers and letters: aaaa1111 AAAAAAAA bbbb2222 BBBBBBBB cccc3333 CCCCCCCC I need to compare x to the numbers in every other line without the... (2 Replies)
Discussion started by: ShiGua
2 Replies

7. UNIX for Dummies Questions & Answers

comparing field to current year

Hi, I have a comma delimited file that contains name, account number, and account date/time(example record below). I want to pull off all the records that have an account date greater than 8/1 of the current year, and create a new file with those records. So for this year, it would take... (6 Replies)
Discussion started by: keeferb
6 Replies

8. Shell Programming and Scripting

Field comparing in files

Guys trying to compare field in two files. For Ex: demo.txt 23.33.4.2 hostname 3.2.4.2 hostname12 demo1.txt 3.3.3.3 hostname23 45.23.23.23 hostname 323 I would like to compare the ips b/w these two files.any... (2 Replies)
Discussion started by: coolkid
2 Replies

9. Shell Programming and Scripting

comparing and inserting common line in other file

Hi, I have two files-- file1- file2:- i have to compare two files and where the uid is same i have to take the password and insert it above the telephone number. the output should be like this-- uid : 1418 common so insert the password thbs above the line telephonenumber. (2 Replies)
Discussion started by: namishtiwari
2 Replies

10. Shell Programming and Scripting

Append a field to the end of each line of a file based on searching another file.

Hi All, I have two comma separated value(CSV) files, say FileA and FileB. The contents looks like that shown below. FileA EmpNo,Name,Age,Sex, 1000,ABC,23,M, 1001,DES,24,F, ... (2 Replies)
Discussion started by: ultimate
2 Replies
Login or Register to Ask a Question