Shell script to retrieve first degree neighbors


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell script to retrieve first degree neighbors
# 1  
Old 12-23-2013
Shell script to retrieve first degree neighbors

I have a file with two columns and each pair in the rows denote 2 connected nodes in the network file, edge_list.txt. Given a query file, input.txt, I want to retrieve the nodes that are directly connected (first degree neighbors) to the nodes present in the input.txt. Kindly help.

edge_list.txt
Code:
A  B
B  J
J  H
C  A
G  H
H  A
K  G

input.txt
Code:
A
G
H

Desired output:

Code:
A  B
C  A
H  A
G  H
K  G
J  H

Please note:
edge_list does not have any duplicate entries. A H and H A are same. The output file cannot have any duplicate entries (i.e. only A H or H A should be present)
Thanks.
# 2  
Old 12-23-2013
Output order will be same as that of edge_list.txt
Try :
Code:
$ awk 'FNR==NR{A[$1];next}($1 in A || $2 in A)' input.txt edge_list.txt  
A  B
J  H
C  A
G  H
H  A
K  G

This User Gave Thanks to Akshay Hegde For This Post:
# 3  
Old 12-23-2013
Do you say that edge_list can't have H A and A H? Then there won't be duplicates in the output anyway. Should above be possible though, remove duplicates with
Code:
awk 'FNR==NR{A[$1];next}($1 in A || $2 in A) && !N[$1,$2] {N[$1,$2]++;N[$2,$1]++; print}' file2 file1

# 4  
Old 12-23-2013
Code:
grep -Ff input.txt edge_list.txt

Regards,
Alister
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How can i run sql queries from UNIX shell script and retrieve data into text docs of UNIX?

Please share the doc asap as very urgently required. (1 Reply)
Discussion started by: 24ajay
1 Replies

2. UNIX for Dummies Questions & Answers

Matching position and output neighbors within 500 distant

Hi, I have been struggling to match positions output its neigbors. Can you please help ? I have 2 files.They both have the same format (same number of columns) but first file is a kind of subset of second file The first file looks like this (tab delimited): 1 11567687 snpid20 1 153881... (4 Replies)
Discussion started by: fat
4 Replies

3. Shell Programming and Scripting

Using shell script to compare files and retrieve connections

Hello, I want to use shell script to generate network files (I tried with python but its taking too long). I have a list of nodes: node.txt LOC_Os11g37970 LOC_Os01g07760 LOC_Os03g19480 LOC_Os11g45740 LOC_Os06g08290 LOC_Os07g02800 I have an edge-list as well: edge.txt Source_node ... (2 Replies)
Discussion started by: Sanchari
2 Replies

4. Shell Programming and Scripting

Unable to retrieve data from shell when connected to Oracle

I am trying to get the data from oracle table to a variable I have tried the below code #/usr/bin/sh echo " I am in correct loop" SPOOL_FILE=/app/scripts/alt.lst sqlplus /nolog <<END_SQL connect bindu/bindu@gis whenever sqlerror exit failure rollback;... (4 Replies)
Discussion started by: Kiransagar
4 Replies

5. UNIX for Dummies Questions & Answers

How to retrieve the value of variable in shell script which is called by crontab

There are two files one is shell script (sample.sh) and another is configuration file (sampl_conf.cfg) configuration file contains one variable $FTP_HOME. the value of this variable vaires for user to user. If user is say jadoo then value is /home/jadoo/ftp/, for user1 - /home/user1/ftp. The... (4 Replies)
Discussion started by: jadoo_c2
4 Replies

6. Shell Programming and Scripting

Retrieve the value of environment variable in shell script which called from crontab

There are two files one is shell script (sample.sh) and another is configuration file (sampl_conf.cfg) configuration file contains one variable $FTP_HOME. the value of this variable vaires for user to user. If user is say jadoo then value is /home/jadoo/ftp/, for user1 - /home/user1/ftp. The... (0 Replies)
Discussion started by: jadoo_c2
0 Replies

7. Shell Programming and Scripting

how to retrieve the word between 2 specific words with shell script

Hi, I have a file content like: : : <span class="ColorRed"> 1.23</span><br> : : the value 1.23 will be changed from time to time, and I want to use a shell script command, e.g grep or sed, to retrieve only the value, how to do it? Thanks! Victor (6 Replies)
Discussion started by: victorcheung
6 Replies

8. Solaris

E450 CPU temperature 80 degree F

Hi all, We are getting CPU warning temeratures sometimes and our server is shutdown at 80 degree F. We notice our server room temperature at 18 degree celcius . Our we have already cleaned dust from grills near CPU Fan . CPU fans are working (moving) . It is our experience that CPU... (5 Replies)
Discussion started by: Hitesh Shah
5 Replies
Login or Register to Ask a Question