awk find closest


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk find closest
# 1  
Old 07-14-2014
awk find closest

Hi All
I am sorry but I have to open a new thread about my issue.
My input files are:

file_1
Code:
ID_1 10
ID_2 15
ID_3 32
ID_4 45
ID_5 66
ID_6 79
ID_7 88

file_2
Code:
ID_3
ID_5

My output file should be

Code:
ID_3 ID_1(-22) ID_2(-17) ID_4(-13) ID_5(34)
ID_5 ID_3(-34) ID_4(-21) ID_6(-13) ID_7(22)

What I would like to obtain is a file like that, in which for each entry in file 2, output the relative ID and the first plus and minus n(set 2 for example) ID(and the difference between column 2) of file 1.
I use this command(lighty modified from Scrutinazer's)

Code:
 awk 'BEGIN{OFS="\t"}NR==FNR{ n=NR; I[n]=$1; L[$1]=n; V[n]=$2;  next;} { j=2; for(i=1; i<=2; i++) { if(i!=L[$1]) {  $(j++)=I[i] "("  V[i]-V[L[$1]] ")" } }}1'

But my output is

Code:
ID_3 ID_1(-22) ID_2(-17)
ID_5 ID_1(-56) ID_2(-51)

That is wrong because I need the closest n (in this case 2) element closest(both up and down).

Any suggestion?
Hope is clear!

Contact me for further information

Thanks!
# 2  
Old 07-14-2014
Code:
awk 'NR == FNR {a[++n] = $1; b[$1] = $2; next}
  {for(i=1; i<=n; i++)
    {if(a[i] == $0)
      {print $0, a[i-2] "(" (b[a[i-2]] - b[a[i]]) ")",
        a[i-1] "(" (b[a[i-1]] - b[a[i]]) ")",
        a[i+1] "(" (b[a[i+1]] - b[a[i]]) ")",
        a[i+2] "(" (b[a[i+2]] - b[a[i]]) ")"}}}' file_1 file_2

This User Gave Thanks to SriniShoo For This Post:
# 3  
Old 07-14-2014
Thanks a lot!It works fine!
But How can I modified the command if I would like to take in consideration only entry in file 1 that have "PC" field in 3th column (but file 2 can conatains any kind of IDs!!):

Code:
ID_1 10 PC
ID_2 15 PC
ID_3 32 PC
ID_4 45 NC
ID_5 66 PC
ID_6 79 PC
ID_7 88 NC

Thanks!
# 4  
Old 07-14-2014
Code:
awk 'NR == FNR && $3 == "PC" {a[++n] = $1; b[$1] = $2; next}
  {for(i=1; i<=n; i++)
    {if(a[i] == $0)
      {print $0, a[i-2] "(" (b[a[i-2]] - b[a[i]]) ")",
        a[i-1] "(" (b[a[i-1]] - b[a[i]]) ")",
        a[i+1] "(" (b[a[i+1]] - b[a[i]]) ")",
        a[i+2] "(" (b[a[i+2]] - b[a[i]]) ")"}}}' file_1 file_2

---------- Post updated at 06:44 AM ---------- Previous update was at 06:44 AM ----------

But, I dont know how you want the output to look like
This User Gave Thanks to SriniShoo For This Post:
# 5  
Old 07-14-2014
ok.
input1
Code:
ID_1 10 PC
ID_2 15 NC
ID_3 32 PC
ID_4 45 NC
ID_5 66 PC
ID_6 79 NC
ID_7 88 NC
ID_8 99 PC
ID_9 101 PC
ID_10 120 PC

input2:
Code:
ID_4
ID_5

OUTPUT(for n=2)

Code:
ID_4 ID_1(distance) ID_3(distance) ID_5(distance) ID_8(distance)
ID_7 ID_3(distance) ID_5(distance) ID_8(distance) ID_9(distance)

That means that I heve to take in consideration the n(2) element that in 3th column have PC field

The previous command have no output

---------- Post updated at 01:38 PM ---------- Previous update was at 01:01 PM ----------

mhhh...it is a completely different command!
But I think that is possible works in another way.
I could have just one file like this:

Code:
ID_1 10 PC
ID_2 15 PC
ID_3 32 NC
ID_4 45 PC
ID_5 66 NC 
ID_6 79 PC 
ID_7 88 PC

Well, for each IDs that in the 3th column contains "NC",I would like to obtain the closest n(set 2 for example) ID and distance (like before) but in which the 3th column is "PC"(skip "NC"IDs).

Suggestion?
# 6  
Old 07-14-2014
Code:
awk 'NR==FNR{
  if($3 == "PC") {a[++n] = $1}
  else {a[n "." m++] = $1};
  b[$1] = $2; next}
  {for(x in a)
    {if(a[x] == $0)
      {y = sprintf("%d", x);
      printf "%s", $0;
      if(x == y)
        {for(i=e; i>=-e; i--)
          {if(i == 0) continue;
          printf "\t%s(%d)", a[y-i], (b[a[y-i]] - b[a[x]])}}
        else {
          for(i=e-1; i>=-e; i--)
            {printf "\t%s(%d)", a[y-i], (b[a[y-i]] - b[a[x]])}}}}
    print ""}' e=2 file_1 file_2

This User Gave Thanks to SriniShoo 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

Print line when closest number if found

so i have a code that identifies which value is the closest to a value provided by the user. awk -F"," -v c=${COLUMN} -v t=${USTIME} '{a=$c}END{ asort(a);d=a-t;d=d<0?-d:d;v = a for(i=NR-1;i>=1;i--){ m=a-t;m=m<0?-m:m if(m<d){ ... (3 Replies)
Discussion started by: SkySmart
3 Replies

2. Shell Programming and Scripting

Matching column and search closest elements

Hi all I have a great challenge that I am not able to resolve. Briefly, I have a file like this: ID_1 chr1 100 - ID_2 chr2 300 + and another file like this: name_1 chr1 150 no - name_2 chr1 250 yes - name_3 chr2 350 yes + name_4 chr2 280 yes + Well, for each entry in file1 I would... (2 Replies)
Discussion started by: giuliangiuseppe
2 Replies

3. SCO

I'm looking for UNIXware 7.xx, or the closest version to that

Im looking for Sco UnixWare 7.xx or the closest version to download it please pm me if you want the privacy (9 Replies)
Discussion started by: BaxxterHp
9 Replies

4. Shell Programming and Scripting

Find the closest value in another csv file preceding it and following it?

Hi, Is this possible? I want to take a csv file and find the closest value in another csv file preceding it and following it. For ex. In this csv file, I'll take the first line: 1309341156.800000000 1309341156.802500000 1309341156.805000000 1309341156.807500000 and find the closest... (2 Replies)
Discussion started by: superbbrr
2 Replies

5. Solaris

What OS closest represents Solaris

Hi all. Im new to programming and Unix. Want to start learning to use Solaris 11. I have 2 boxes currently and want to run an all in one Solaris Box to act as a file server, web server ect. I want to use the second box as my main programming box that is as close to a Solaris OS as possible that has... (3 Replies)
Discussion started by: Fingerz
3 Replies

6. Shell Programming and Scripting

Increment and find closest value until the end of the file is reached.

Hi all, I have a file which looks like: 0 1.1985506 1 1.2237930 2 1.2159038 3 1.2668828 4 1.2650216 5 1.2474344 6 1.2817688 7 1.2721698 8 1.2665005 9 1.2826315 10 1.2797879 11 1.3201736 12 1.3116595 13 1.3361583 14 1.3309238 (2 Replies)
Discussion started by: ezitoc
2 Replies

7. Shell Programming and Scripting

Closest Number from a Range of Numbers

out of a range of numbers, how can i pick out the number that is the closest to any arbitrary/random number that a user supplies? say the range of numbers are between 1 - 90000. but that doesn't mean each number exist between 1 - 90000. the range of numbers could be for example: 1, 3, 4, 6,... (6 Replies)
Discussion started by: SkySmart
6 Replies

8. Shell Programming and Scripting

delete rows between closest pattern or range

Hi I am having some problom deleting the lines between two specific lines in a file. need to delete lines between two closest lines. i.e need to find the closest range or pattern in a file with repeating patterns. Sample Input: WARNING <some text in n number of lines> ERROR:2597... (10 Replies)
Discussion started by: sudheer1984
10 Replies

9. UNIX for Advanced & Expert Users

sed match closest/nearest pattern

All i am struggling to raplace some text in a line between two (closest) patterns , line="/home/usr/bin/:/home/usr/devuser,n1.9/bin:/home/usr/root/bin" i want to replace "devuser,n1.9" with "NEWVAL", basically all teh text from "devuser" until nearest '/' with some new text. i tried teh... (1 Reply)
Discussion started by: sudheer1984
1 Replies

10. Shell Programming and Scripting

awk or sed for finding closest pattern to a line number

hi guys, I want to do pattern matching with awk or sed but I don't know how. here's what I want: I have a line number for a pattern that I have already found using grep, and I know a pattern like "---" that happens a few lines above that certain line number. I want to print out the chunk... (1 Reply)
Discussion started by: alirezan
1 Replies
Login or Register to Ask a Question