merge file with nearest range !!!!!!!


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting merge file with nearest range !!!!!!!
# 8  
Old 02-12-2009
find nearest

Hi,

I'm trying to find the nearest match between two columns of numbers, e.g.

1,1
10,8
30,50
20,100

and the search could be e.g. 20,20

returning 10,8 - i.e. 20-10 = 10 and 20-8 = 12 totalling 22, and hence being the nearest match.

any ideas?

thanks a lot,


# 9  
Old 02-13-2009
find the closest numerical value -perl

>is it possible, with perl, to find the closest numerical value to a set value.
>>IE. a set of va;lues in column1 (a set value of 15) and I have another column with some set of values (five values, 208,258,56,123 )
>>
>I made perl script too go through the five values array to find the closest to
>>15 .
Iwant to assign this to column1 column 2 with thousands of values. Is it possible to assign this same script to col1 and col2?????

use strict;
> use warnings;
>
> my $set = 15;
> my @vals = (208, 258, 56, 123);
>
> print closest($set, @vals), "\n";
>
> sub closest {
> my $val = shift;
> my @list = sort { abs($a - $val) <=> abs($b - $val) } @_;
> $list[0];
> }
>

thanx alot in advance
# 10  
Old 02-13-2009
Hi bogu,

I´m not sure, I think you want the value in column1 that is the most similar or nearest from every value in column 2.

Well, I´m thinking about an algorithm that could work assuming the values in both columns are in ascending order.

Experts, may somebody correct me?. I´m new with awk, I´ve only got so far the code showed below, which it´s not working yet.
I hope the logic makes senseSmilie

Code:
awk '{a[i]=$1;b[i]=$2;next} 
{ for (j=1;j<=i;j++) 
		{ 
			if(a[j]<=b[j] && b[j]>=a[j+1]) {$3==a[i]; print;break}
		}
}' Inputfile

My idea in this code is to find out where the value "i" of column 2 is between 2 consecutive values in column 1, and then copy in column 3 the first value of those 2 consecutive values.

Thanks for any cooperationSmilie
# 11  
Old 02-13-2009
thanx alot for the co operation . lets hope for the perfect answer from some experts.
# 12  
Old 02-15-2009
join the nearest values in two files

Hi guys
I have 2 files with different numbers. I would like to join the nearest possible values. for example

File 1

3000000
100000000
110000000
120000000

File 2

2908791
115194157
114942245
114942510
9830920
86792476
116181989
173550955
82609186
100487233

out put

2908791 3000000
115194157 110000000

some thiiig like that

Thanx alot in advance
# 13  
Old 02-15-2009
perl script to join the the nearest values in two files

file 1
150
350


file 2

100
200
300
400
500

output
150 200
350 400
# 14  
Old 02-15-2009
In editing – awk script -Not working

My idea in this code is to find out where the value "i" of column 2 is between 2 consecutive values in column 1, and then copy in column 3 the first value of those 2 consecutive values.

awk '{a[i]=$1;b[i]=$2;next}
{ for (j=1;j<=i;j++)
{
if(a[j]<=b[j] && b[j]>=a[j+1]) {$3==a[i]; print;break}
}
}' Inputfile
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Checking File record equal to multiple of 70 or nearest number to multiple of 70

Hello, I have a file with below content - Example 3 6 69 139 210 345 395 418 490 492 I would like the result as - Multiple of 70 or nearest number in the file less than the multiple of 70 69 139 (5 Replies)
Discussion started by: Mannu2525
5 Replies

2. Shell Programming and Scripting

Match and Grep the nearest value in last field

Gents I have this input file file1 (uniq records) 54503207851 170211240 54503207911 170210837 54503208111 170215105 54503208112 170215210 54655210011 170223140 54655210091 170223738 54655210172 170224355 54655210251 170224741 54655210331 170225039 54655210411 170225505 54655210492... (13 Replies)
Discussion started by: jiam912
13 Replies

3. Shell Programming and Scripting

Selecting nearest pattern match

I'm looking to match an error code against a list of possible codes and get the nearest match. The code would be a 6 character hexadecimal string. I have a file of error codes all of which have a specific first 3 characters, however, after that the last 3 characters may be specific or generic as... (3 Replies)
Discussion started by: dazedandconfuse
3 Replies

4. Shell Programming and Scripting

Rounding off to the nearest floating number

I have a number, which I want to convert into the nearest floating number upto two places after the decimal point. E.g. 1.2346 will become 1.23 but 1.2356 will become 1.24 . Similarly 0.009 will be 0.01 and 0.001 will be 0.00 or 0.0 (not 0, wnat to keep the decimal... (1 Reply)
Discussion started by: hbar
1 Replies

5. UNIX for Dummies Questions & Answers

Rounding up to nearest whole number

Hi all of you, Would be great if you help me with how to round up to whole number from my input values like 2.99996,2.17890,3.00002,-2.3456,-2.7890 o/p should be like 3,2,3,-2,-3 thnks in adv!!!! regards (3 Replies)
Discussion started by: Indra2011
3 Replies

6. Shell Programming and Scripting

Grep the nearest value from a csv file to another

Hello, I have two csv file. The first one (a huge file) with 3 columns X, Y, Z The second one with 2 columns A, B All the values are numbers. I need to parse every "Z" field of the first file, find the closest "B" value in the second and extract from it the "A" relative value, to... (5 Replies)
Discussion started by: rjval
5 Replies

7. Homework & Coursework Questions

Need Help, How to round off to the nearest 5 sen?

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data:#include <stdio.h> #define GTAX 0.06 #define STAX 0.10 int main(void) { int C_sets; double Price_C; ... (1 Reply)
Discussion started by: pwmk
1 Replies

8. UNIX for Dummies Questions & Answers

finding nearest value in a column

Hi, I have 2 files: file1: 1 ia 2 1 mn 6 1 sd 11 2 ny 3 2 ma 10 3 wa 7 3 ca 8 file2 1 mi 3 1 wi 5 2 pa 4 3 id 6 (2 Replies)
Discussion started by: peanuts48
2 Replies

9. UNIX for Dummies Questions & Answers

match nearest

Hi, I'm trying to find the nearest match between two columns of numbers, e.g. 1,1 10,8 30,50 20,100 and the search could be e.g. 20,20 returning 10,8 - i.e. 20-10 = 10 and 20-8 = 12 totalling 22, and hence being the nearest match. any ideas? thanks a lot, (1 Reply)
Discussion started by: bogu0001
1 Replies

10. Shell Programming and Scripting

Urgent help on grep the nearest value

Hi, I have a list of data in text files with :- ColA ColB ColC ColD ColE ColF 12 122 2280 30 0.012987 1.776 13 118 2280 30 0.012987 1.954 14 115 2620 30 0.011321 2.131 15 113 2654 30 0.011177 2.309 16 145 1882 25 0.013110 2.487 17 112... (2 Replies)
Discussion started by: ahjiefreak
2 Replies
Login or Register to Ask a Question