Fetch parent value based on column values

Hi All,

I am trying to achieve the below logic, could you please help me in this.

In second row 2nd column I've Value JC2 and the same JC2 is 4th row 1st column.So I need to replace JC2 value in 4th row with JC2 2nd row's 1st column.

Input:

Job1,JC1
Job1,JC2
Job1,JC3
JC2,JA1
JC2,JA4
JG,KA
JA1,JJ

Output:

Job1,JC1
Job1,JC2
Job1,JC3
Job1,JA1
Job1,JA4
JG,KA
Job1,JJ

Thanks & Regards,
Ulf

Any attempts from your side?

Dear unme,
I have a few to questions pose in response first:-

  • Is this homework/assignment? There are specific forums for these.
  • What have you tried so far?
  • What output/errors do you get?
  • What OS and version are you using?
  • What are your preferred tools? (C, shell, perl, awk, etc.)
  • What logical process have you considered? (to help steer us to follow what you are trying to achieve)

Most importantly, What have you tried so far?

There are probably many ways to achieve most tasks, so giving us an idea of your style and thoughts will help us guide you to an answer most suitable to you so you can adjust it to suit your needs in future.

We're all here to learn and getting the relevant information will help us all.

Its not assignment, as part of my regular work I am doing it... Some how tried to achieve the same using C program with 8 input records.

#include<stdio.h>

char emp[7][3] = {{'A', 'B'},
                  {'A', 'C'},
                  {'A', 'D'},
                  {'C', 'E'},
                  {'C', 'F'},
                  {'X', 'Y'},
                  {'E', 'Z'}};
char topMgr;
                  
char findTopMgr(char mgr, char sub)
{
     int i,j;
/*     printf("\n Mgr=%c, Sub=%c\n", mgr, sub);*/
     for(i=0;i<7;i++)
     {
                     if(mgr == emp[1]) //found top mgr for mgr
                     {
                            sub = mgr; // mgr becomes sub &
                            mgr = emp[0];
                            break;
                     }                            
     }
     
     if(i == 7)// not found top mgr
     {
          /*printf("not found top mgr for %c \n", mgr);*/
          topMgr = mgr;
          return mgr;
          }
     else
         findTopMgr(mgr, sub);
}



int main()
{
    int i,j,c;
    
    char mgr,sub;
    
    
    printf("Source\n");
    
    for(i=0; i<7; i++)
    {
             for (j=0;j<2;j++)
             {
                 printf("%c",emp[j]);
                 printf("\t");
             }
             printf("\n");
    }
    
    //////////////////////////////
    for(i=0;i<7;i++)
    {
    /* printf("%d row -----------\n", i+1);*/
                        sub = emp[1];
                        mgr = emp[0];
                        findTopMgr(mgr, sub);
                        emp[2] =  topMgr;    // top mgr in last column
                        
    }
    
    //////////////////////////////////
    
    printf("Output\n");
       
    for(i=0; i<7; i++)
    {
             for (j=1;j<3;j++)
             {
                 printf("%c",emp[j]);
                 printf("\t");
             }
             printf("\n");
    }
    
    getch();
    
    }

I am getting the desired output, but I am trying to achieve the same using shell script. Could you please help me how to use arrays in unix:

Thanks a lot

---------- Post updated at 12:55 AM ---------- Previous update was at 12:47 AM ----------

Also tried to read the data from file to provide input to the above code. Currently I am trying to integrate the below code into the previous thread code.

#include <stdio.h>
#include <string.h>

char str1[8][20], str2[8][20]; 
char emp[8][2][20];

void main()
{
    FILE *fp = fopen("user.txt", "r");
    const char s[2] = ",";
    char *token;
    int i, index=0;
    
    if(fp != NULL)
    {
        char line[20];
        while(fgets(line, sizeof(line), fp) != NULL)
        {
            token = strtok(line, s);
            strcpy(str1[index], token);
            strcpy(emp[index][0], token);
            for(i=0;i<2;i++)
            {                       
                            
                if(i==0)
                {   
         //           printf("%s  ",token);
                    token = strtok(NULL,s);
                    strcpy(str2[index], token);
                    strcpy(emp[index][1], token);
                } else {
 //                   printf("%s\n",token);
                }       
                
            }
            index++;
        }
        fclose(fp);
    } else {
        perror("user.txt");
    }
   // printf("\n-------------------\n");
    for(i=0;i<8;i++)
    {                
                     //printf("%d %s %s \n", i+1, str1, str2); 
                     printf("%s,%s \n", emp[0], emp[1]);             
    }
 
getch();   
}   

My O/S is GNU LINUX & I am trying to create the same logic in korn/bash shell.

Personally, when shell scripting using arrays for things like this, I prefer awk :

#!/bin/ksh
awk '
BEGIN {	FS = OFS = "," }
NR == FNR {
	tr[$2] = $1; next
}
{	while($1 in tr) $1 = tr[$1] }
1' file file

When file contains:

Job1,JC1
Job1,JC2
Job1,JC3
JC2,JA1
JC2,JA4
JG,KA
JA1,JJ

as shown in post #1 in this thread, the output is:

Job1,JC1
Job1,JC2
Job1,JC3
Job1,JA1
Job1,JA4
JG,KA
Job1,JJ

which, I believe, is what you wanted.

1 Like

Thanks a lot, really its best solution. I took long time to write c program, But your solution is very compacted really thanks a ton.

For data exactly like your sample data, this simple approach might do as well:

awk -F, ' $1 in X {$1=X[$1]} {X[$2]=$1} 1' OFS="," file
Job1,JC1
Job1,JC2
Job1,JC3
Job1,JA1
Job1,JA4
JG,KA
Job1,JJ
1 Like