Collapse linked values


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Collapse linked values
# 1  
Old 04-09-2015
Collapse linked values

please help, I want to group together all linked data pairs.

If I have 10 pairs, each row showing col2 and col3 are linked.

Code:
R1 1 2
R2 1 3
R3 2 4
R4 3 4
R5 5 6
R6 8 1
R7 6 7
R8 9 10

Then I am looking to make

Code:
R1 1 2 3 4 8
R5 5 6 7
R8 9 10


The first col value can be chosen from any of the linked row labels, I have used the first one here.

Here is my try
Code:
awk  'NR==FNR{a[$2]=$3 ; a[$3]=$2;next} $2" "$3 in a {  print $1,a[$2" "$3]}'  file file

# 2  
Old 04-10-2015
Interesting problem... First attempt:
Code:
awk '
  $2 in A {
    A[$3]=A[$2]
    next
  } 
  $3 in A {
    A[$2]=A[$3]
    next
  }
  {
    A[$2]=A[$3]=$1
  }
  END {
    for(i in A) B[A[i]]=B[A[i]] i FS
    for(i in B) print i, B[i]
  }
' file

Output:
Code:
R1 2 3 4 8 1 
R5 5 6 7 
R8 9 10

But this does not address yet when two separate chains are linked by a new pair...

Last edited by Scrutinizer; 04-10-2015 at 03:44 AM..
This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 04-10-2015
This should fix that:
Code:
awk '
  $2 in A {
    o=A[$3];
    for(i in A) if (A[i]==o) A[i]=A[$2]
    next
  } 
  $3 in A {
    A[$2]=A[$3]
    next
  }
  {
    A[$2]=A[$3]=$1
  }
  END {
    for(i in A) B[A[i]]=B[A[i]] i FS
    for(i in B) print i, B[i]
  }
' file

This User Gave Thanks to Scrutinizer For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

7 More Discussions You Might Find Interesting

1. Programming

Help with linked list.

#include<stdio.h> #include<stdlib.h> struct LinkedList { int val; struct LinkedList *next; }node; /*Creating a structure variable*/ typedef struct LinkedList Node; Node *start = NULL; int create(int i) { Node *temp = NULL; if (start == NULL) ... (5 Replies)
Discussion started by: prinsh
5 Replies

2. UNIX for Dummies Questions & Answers

simple code to collapse rows in bash

Hello to the experts! I have a file that I'd like to collapse based on a common ID column, separated by a character delimiter. example input a 1 6 word1 uniq1 b 2 7 WORD2 uniq2 b 2 7 WORD2 uniq3 b 2 7 WORD2 uniq4 c 3 8 word4 uniq5 d 4 9 word5 uniq6 e 5 1 word6 uniq7 desired output a 1... (3 Replies)
Discussion started by: torchij
3 Replies

3. UNIX for Dummies Questions & Answers

Linked Servers

Hi, We have 2 UNIX Servers, say test1 and test2. Here, if I create a file or folder/delete a file or folder in the 1st server, it gets reflected automatically in the 2nd server. I don't think any links are established between these 2 servers. Both these have 2 different hostnames. How... (1 Reply)
Discussion started by: Dev_Dev
1 Replies

4. Programming

Help with linked list in C

i have this code typedef struct client_list { char *client_name; struct client_list * next; int client_socket_fd; } client; client *current, *head; head = NULL; char *h="test"; add_client(current, h, head, &client_socket_fd); ... (24 Replies)
Discussion started by: omega666
24 Replies

5. UNIX for Advanced & Expert Users

block editing, collapse, comment.

any way i can block edit a program ? i wrote a macro to do it in emacs so it works like : void foo ( int ... ) ; // collapsed. it moves the body to temperary buffer, but i can't rely on this :( (2 Replies)
Discussion started by: max_475
2 Replies

6. UNIX for Dummies Questions & Answers

is there any way to see what and who is linked to file?

Hello like the topic says... thanks (2 Replies)
Discussion started by: umen
2 Replies

7. UNIX for Advanced & Expert Users

dynamically linked file

Hi friends, i have a dynamically linked file on my solaris system.this is script that runs regularly. How can i read the contents of that ? when i tried to say "vi filename " then it says executable and nothing is seen. Please help. thanks in advance Veera (5 Replies)
Discussion started by: sveera
5 Replies
Login or Register to Ask a Question