Interpolation if there is no exact match for value


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Interpolation if there is no exact match for value
# 15  
Old 05-09-2014
@vgersh99 Thanks I just updated your suggestion in original code...
# 16  
Old 05-09-2014
one more time, thank a lot to all of u, will work with codes, it takes time for me
# 17  
Old 05-09-2014
Another one, which assumes that the x values are in ascending order and it will interpolate and extrapolate if x is beyond the boundaries in file2
Code:
awk '
  FNR==1 {
    next                                          # skip the headers
  }

  FNR==NR {                                       # when reading the first file
    P[$1]=$2                                      # fill array P with points
    I[i++]=$1                                     # fill array I with point indices
    next
  }

  {
    while (j==0 || ($1>t && j+1 in I)) {          # determine if shift is needed to next index, do not shift beyond rightmost
      s=I[j]                                      # set s to next left index
      t=I[j+1]                                    # set t to next right index
      j++                                         # increase index
    }
    print $1, $2, P[s]+($1-s)*(P[t]-P[s])/(t-s)   # interpolate / extrapolate and print
  } 
' file2 file

Ouput:
Code:
752566 rs3094315 2.01296
752721 rs3131972 2.01314
753541 rs2073813 2.01413
760300 rs11564776 2.01695


Last edited by Scrutinizer; 05-09-2014 at 05:53 PM..
This User Gave Thanks to Scrutinizer For This Post:
# 18  
Old 05-10-2014
Some more way...by sorting...

Code:
awk '
FNR==NR && FNR >1 {
		    # Read first file starting from 2nd line
    		    A[$1]=$2
		    next
	          }
  
   NR!=1 && FNR==1{
		    # Reading 2nd file
		    # Sort Array index,print current line line
		    asorti(A,tmp)
		    print 
	          }

    NR!=1 && FNR>1{
			# This is index key
			key = $1

			# If exact match print as it is..
			if(key in A)
			{
				print key,A[key]
			}
	 		else
			{
				# No match search for upper and lower 
				for(i in tmp)
				{
				   if (tmp[i]>key)
				      { 
					x3 = tmp[i]
					break
				      }
				}

				x1 = tmp[i-1]

			# if x1 < x2 < x3 then interpolate 
			if(x1 < key && x3 >key)
				{

				   y1 = A[x1]
				   y3 = A[x3]
				   x2 = key
				   y2 = (y1+(x2-x1)*(y3-y1)/(x3-x1))

				   print x2,y2
				}
			else
				{
				   # print NaN as we cannot interpolate
				   print key,"NaN"
				}

		}
		
    	         }
    '  file2 file1


In GNU Awk 4 you could use PROCINFO["sorted_in"]="@ind_num_asc" to sort indices in ascending order.
This User Gave Thanks to Akshay Hegde For This Post:
# 19  
Old 05-12-2014
Quote:
Originally Posted by Scrutinizer
Another one, which assumes that the x values are in ascending order and it will interpolate and extrapolate if x is beyond the boundaries in file2
Code:
awk '
  FNR==1 {
    next                                          # skip the headers
  }

  FNR==NR {                                       # when reading the first file
    P[$1]=$2                                      # fill array P with points
    I[i++]=$1                                     # fill array I with point indices
    next
  }

  {
    while (j==0 || ($1>t && j+1 in I)) {          # determine if shift is needed to next index, do not shift beyond rightmost
      s=I[j]                                      # set s to next left index
      t=I[j+1]                                    # set t to next right index
      j++                                         # increase index
    }
    print $1, $2, P[s]+($1-s)*(P[t]-P[s])/(t-s)   # interpolate / extrapolate and print
  } 
' file2 file

Ouput:
Code:
752566 rs3094315 2.01296
752721 rs3131972 2.01314
753541 rs2073813 2.01413
760300 rs11564776 2.01695


May I ask one more addition to this code? the number of digits in output in the third column is always 6, meaning the bigger number becomes, less decimal digits remain: e.g. when number in third column increases till 100 then i only have three digits after comma, but it is important, that I still get 6 digits after comma.
Could you, please, help with this also?
Sorry for bothering with such questions.
And thanks a lot to all in this very helpful forum!
# 20  
Old 05-12-2014
Code:
....
' OFMT='%.6f'  file2 file

This User Gave Thanks to vgersh99 For This Post:
# 21  
Old 05-12-2014
vgersh99, thank you very much!
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to update file based on partial match in field1 and exact match in field2

I am trying to create a cronjob that will run on startup that will look at a list.txt file to see if there is a later version of a database using database.txt as the source. The matching lines are written to output. $1 in database.txt will be in list.txt as a partial match. $2 of database.txt... (2 Replies)
Discussion started by: cmccabe
2 Replies

2. Shell Programming and Scripting

Replacing exact match

Hi All, My Input file contains a 1000’s of lines in which I have to replace a a string to the other. Here the problem is, I have the lines in my Input as below. Cable Yes && !Pay TV && !ADS \noUE \Label="Cable Yes && !Pay TV && !ADS" I want to replace exactly the string Cable Yes &&... (37 Replies)
Discussion started by: am24
37 Replies

3. UNIX for Dummies Questions & Answers

Exact match question

Hi guys, I am using Centos 6.3. Actually I posted similar question but I still have some minor problem need be fixed. I have two files, file1:target: gi|57529786|ref|NM_001006513.1| mfe: -31.4 kcal/mol p-value: 0.006985 target: gi|403048743|ref|NM_001271159.1| mfe: -29.6 kcal/mol p-value:... (11 Replies)
Discussion started by: yuejian
11 Replies

4. Shell Programming and Scripting

Exact match using sed

I would like replace all the rows in a file if a row has an exact match to number say 21 in a tab delimited file. I want to delete the row only if it has 21 any of the rows but it should not delecte the row that has 542178 or 563421. I tried this sed '/\<21\>/d' ./inputfile > output.txt ... (7 Replies)
Discussion started by: Kanja
7 Replies

5. Shell Programming and Scripting

Match exact and append zero

file 11 2 12 6 13 7 114 6 011 7 if I'm searching for 11, output needed is output: 11 2 011 7 Code: awk '$1 ~ /^11$/' file I used the above to match exact, but it avoiding "011 7" line too, how to resolve this? (6 Replies)
Discussion started by: Roozo
6 Replies

6. Shell Programming and Scripting

Exact match and #

Hi friends, i am using the following grep command for exact word match: >echo "sachin#tendulkar" | grep -iw "sachin" output: sachin#tendulkar as we can see in the above example that its throwinng the exact match(which is not the case as the keyword is sachin and string is... (6 Replies)
Discussion started by: neelmani
6 Replies

7. Solaris

grep exact match

Hi This time I'm trying to grep for an exact match e.g cat.dog.horse.cow.bird.pig horse.dog.pig pig.cat.horse.dog horse dog dog pig.dog pig.dog.bird how do I grep for dog only so that a wc -l would result 2 in above case. Thanks in advance ---------- Post updated at 06:33 AM... (4 Replies)
Discussion started by: rob171171
4 Replies

8. Shell Programming and Scripting

Exact match question

Hi, I have a file like follows . . . White.Jack.is.going.home Black.Jack.is.going.home Red.Jack.is.going.home Jack.is.going.home . . . when I make: cat <file> | grep -w "Jack.is.going.home" it gives: White.Jack.is.going.home Black.Jack.is.going.home Red.Jack.is.going.home... (4 Replies)
Discussion started by: salih81
4 Replies

9. Shell Programming and Scripting

exact string match ; search and print match

I am trying to match a pattern exactly in a shell script. I have tried two methods awk '/\<mpath${CURR_MP}\>/{print $1 $2}' multipath perl -ne '/\bmpath${CURR_MP}\b/ and print' /var/tmp/multipath Both these methods require that I use the escape character. I am guessing that is why... (8 Replies)
Discussion started by: bash_in_my_head
8 Replies

10. Shell Programming and Scripting

perl exact match

How to emulate grep -o option in perl. I mean to print not all line, only the exact match. echo "2A2 BB" | perl -ne 'print if /2A2/' 2A2 BB I want to print only 2A2. (2 Replies)
Discussion started by: mirusnet
2 Replies
Login or Register to Ask a Question