Find first n element by matching IDs


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find first n element by matching IDs
# 1  
Old 07-11-2014
Find first n element by matching IDs

Hi All
I have a problem that I am not able to resolve.
Briefly, I have a file like this:

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

This file is numerically ordered for the 2th column.
And another file containing a list of IDs(just one in this example)

Code:
ID_4

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 3 for example) ID(and the difference between column 2) of file 1.

Code:
ID_4 ID_1(-35) ID_2(-20) ID_3(-13) ID_5(21) ID_6(34) ID_7(43)

If you need further details do not hesitate to contact me!
Thank you!

PS:There are same cases of different IDs and same number.In this case just refer to the order of file.
# 2  
Old 07-11-2014
Hi, try:
Code:
awk '
  NR==FNR {
    n=NR
    I[n]=$1
    L[$1]=n
    V[n]=$2
    next
  }
  {
    j=2
    for(i=1; i<=n; i++) { 
      if(i!=L[$1]) {
        $(j++)=I[i] "(" V[i]-V[L[$1]] ")"
      }
    }
  }
  1
' file1 file2


Last edited by Scrutinizer; 07-11-2014 at 01:50 PM..
This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 07-11-2014
Thank you for the help!
Unfortunately the awk command does not work(^ syntax error)!!
I tried also with the list posted above.
Any other suggestion?
Thank you
# 4  
Old 07-11-2014
What is your OS ands version?
This User Gave Thanks to Scrutinizer For This Post:
# 5  
Old 07-11-2014
Hi
Unfortunately now I am not at work and I do not remember the version :-(
However I am working on debian OS.
# 6  
Old 07-11-2014
I noticed that something had gone wrong with posting the suggestion. I corrected it, perhaps you could try again...
This User Gave Thanks to Scrutinizer For This Post:
# 7  
Old 07-11-2014
Thank you Scrutinizer for following my issue!
As soon as I will have the possibility I will run the command and I ll let you know!
Best
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

List of all ids,groups, privilege ids

I wish to pull out a list of all user ids on the system, including the privileged ids, the groups to which they belong to. Sometimes after deleting an id also, its home dir does not get deleted or an entry is left behind in /etc/passwd. Can someone help me with a script to achieve both. (2 Replies)
Discussion started by: ggayathri
2 Replies

2. Shell Programming and Scripting

awk does not find ids with semi-colon in the name

I am using awk to search $5 of the "input" file using the "list" file as the search criteria. So if the id in line 1 of "list" is found in "search" then it is counted in the ids found. However, if the line in "list" is not found in "search", then it is outputted as is missing. The awk below runs... (3 Replies)
Discussion started by: cmccabe
3 Replies

3. Shell Programming and Scripting

Script/command to find the common element from 2 reports

please look the scenario (8 Replies)
Discussion started by: dll_fpga
8 Replies

4. Shell Programming and Scripting

Script/command to find the common element from 2 reports

Please see the below reports.... The startpoint will be always same but the endpoint is different ,across various reports and im looking for a search between "clock network delay(propagated)" and "data arrival time" ....to find the element which is common in both reports(reports are seperated... (0 Replies)
Discussion started by: dll_fpga
0 Replies

5. UNIX for Dummies Questions & Answers

Extract columns by matching ids in two files

Hello, I want to extract columns from file2 to file3 by matching ids between file1 and file2. The extracted columns should be in same order as file1 ids. for example: file1.txt 1823 607 R2A9 802 771 file2.txt 1823 1 2 4 22 11 4 29 607 12 3 3 R2A9... (8 Replies)
Discussion started by: ryan9011
8 Replies

6. Shell Programming and Scripting

Find if XML element has a matching required element

I want to check if every <Part> element has corresponding <Description> in this sample XML. ....<Lot Of XML> <Inv lineNumber="2"> <Item> ... (4 Replies)
Discussion started by: kchinnam
4 Replies

7. Shell Programming and Scripting

Need to find root element name of XML file

Given this XML: <?xml version="1.0"?> <catalog> <cd> <title>Empire Burlesque</title> <artist>Bob Dylan</artist> <country>USA</country> <company>Columbia</company> <price>10.90</price> <year>1985</year> </cd> <cd> <title>Hide your heart</title> ... (2 Replies)
Discussion started by: ricksj
2 Replies

8. UNIX for Dummies Questions & Answers

matching IDs from two files and editting

I have two files. One has: ID# 0 a b c d e f g h i j k....................~2 milion columns ID# 0 l m n o p q r s t u v....................~2 milion columns . . . ~6000 lines Other has: ID# 1 or ID# 2 . . ~6000 lines (2 Replies)
Discussion started by: polly_falconer
2 Replies

9. Shell Programming and Scripting

Error to "find" a matching array element in a directory

Hi, I have defined an array which holds a couple of elements which are nothing but files names. I want to find the files in a directory for the matching file name(array elements) with less than 1 day old. When I am trying to execute the code (as below), it gives an error. Your help in this... (1 Reply)
Discussion started by: mkbaral
1 Replies

10. Shell Programming and Scripting

Need to find Unix ids

Hi How can find the Unix ids for couple of users i am not sure of the command , can anyone help me on this :) (1 Reply)
Discussion started by: raghav1982
1 Replies
Login or Register to Ask a Question