Fetch parent value based on column values


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Fetch parent value based on column values
# 1  
Old 02-19-2015
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:
Code:
Job1,JC1
Job1,JC2
Job1,JC3
JC2,JA1
JC2,JA4
JG,KA
JA1,JJ

Output:
Code:
Job1,JC1
Job1,JC2
Job1,JC3
Job1,JA1
Job1,JA4
JG,KA
Job1,JJ

Thanks & Regards,
Ulf
# 2  
Old 02-19-2015
Any attempts from your side?
# 3  
Old 02-23-2015
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.
# 4  
Old 03-04-2015
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.
Code:
#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[i][1]) //found top mgr for mgr
                     {
                            sub = mgr; // mgr becomes sub &
                            mgr = emp[i][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[i][j]);
                 printf("\t");
             }
             printf("\n");
    }
    
    //////////////////////////////
    for(i=0;i<7;i++)
    {
    /* printf("%d row -----------\n", i+1);*/
                        sub = emp[i][1];
                        mgr = emp[i][0];
                        findTopMgr(mgr, sub);
                        emp[i][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[i][j]);
                 printf("\t");
             }
             printf("\n");
    }
    
    getch();
    
    }

Image

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.
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[i], str2[i]); 
                     printf("%s,%s \n", emp[i][0], emp[i][1]);             
    }
 
getch();   
}

My O/S is GNU LINUX & I am trying to create the same logic in korn/bash shell.
# 5  
Old 03-04-2015
Personally, when shell scripting using arrays for things like this, I prefer awk:
Code:
#!/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:
Code:
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:
Code:
Job1,JC1
Job1,JC2
Job1,JC3
Job1,JA1
Job1,JA4
JG,KA
Job1,JJ

which, I believe, is what you wanted.
This User Gave Thanks to Don Cragun For This Post:
# 6  
Old 03-04-2015
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.
# 7  
Old 03-05-2015
For data exactly like your sample data, this simple approach might do as well:
Code:
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

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

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Filtering based on column values

Hi there, I am trying to filter a big file with several columns using values on a column with values like (AC=5;AN=10;SF=341,377,517,643,662;VRT=1). I wont to filter the data based on SF= values that are (bigger than 400) ... (25 Replies)
Discussion started by: daashti
25 Replies

2. Shell Programming and Scripting

Concatenate values in the first column based on the second column.

I have a file (myfile.txt) with contents like this: 1.txt apple is 3.txt apple is 5.txt apple is 2.txt apple is a 7.txt apple is a 8.txt apple is a fruit 4.txt orange not a fruit 6.txt zero isThe above file is already sorted using this command: sort -k2 myfile.txtMy objective is to get... (3 Replies)
Discussion started by: shoaibjameel123
3 Replies

3. Shell Programming and Scripting

Fetch the values based on a Key using awk from single file

Hi, Please help to fetch the values for a key from below data format in linux. Sample Input Data Format 11055005|PurchaseCondition|GiftQuantity|1 11055005|PurchaseCondition|MinimumPurchase|400 11055005|GiftCatalogEntryIdentifier|Id|207328014 11429510|PurchaseCondition|GiftQuantity|1... (2 Replies)
Discussion started by: mohanalakshmi
2 Replies

4. UNIX for Dummies Questions & Answers

Repositioning based on column values

Dear all ... I have a file which I want to change the structure based on the values in some columns and I would be grateful if you can help... one of my files looks like ... they all have ten rows 1,0,0 10,0,0 2,0,0 3,0,0 4,1,1 4,1,1 4,1,1 5,0,0 6,0,0 7,0,0 8,0.5,2 9,0.33,3 9,0.33,3... (1 Reply)
Discussion started by: A-V
1 Replies

5. Shell Programming and Scripting

Sum column values based in common identifier in 1st column.

Hi, I have a table to be imported for R as matrix or data.frame but I first need to edit it because I've got several lines with the same identifier (1st column), so I want to sum the each column (2nd -nth) of each identifier (1st column) The input is for example, after sorted: K00001 1 1 4 3... (8 Replies)
Discussion started by: sargotrons
8 Replies

6. Shell Programming and Scripting

Adding values of a column based on another column

Hello, I have a data such as this: ENSGALG00000000189 329 G A 4 2 0 ENSGALG00000000189 518 T C 5 1 0 ENSGALG00000000189 1104 G A 5 1 0 ENSGALG00000000187 3687 G T 5 1 0 ENSGALG00000000187 4533 A T 4 2 0 ENSGALG00000000233 5811 T C 4 2 0 ENSGALG00000000233 5998 C A 5 1 0 I want to... (3 Replies)
Discussion started by: Homa
3 Replies

7. Shell Programming and Scripting

Deleting values in a column based on conditions

Hi I have a difficulty in writing shell script for performing the tasks. A B C D 12 230 16 259 18 260 23 283 21 291 36 298 41 309 49 420 52 425 57 450 61 456 70 473 72 475 79 486 If the A(row no.2) < C(row no.1) then delete value A(row no.1) and so on... For... (8 Replies)
Discussion started by: Sarwagya Jha
8 Replies

8. UNIX for Dummies Questions & Answers

Cut from tables based on column values

Hello, I have a tab-delimited table that may contain 11,12 or 13 columns. Depending on the number of columns, I want to cut and get a sub table as shown below. However, the awk commands in the code seem to be an issue. What should I be doing differently? #cut columns 1-2,4-5,11 when 12 &... (3 Replies)
Discussion started by: Gussifinknottle
3 Replies

9. Shell Programming and Scripting

How to averaging column based on first column values

Hello I have file that consist of 2 columns of millions of entries timestamp and throughput I want to find the average (throughput ) for each equal timestamp before change it to proper format e.g : i want to average 2 coloumnd fot all 1308154800 values in column 1 and then print... (4 Replies)
Discussion started by: aadel
4 Replies

10. Shell Programming and Scripting

How to pick values from column based on key values by usin AWK

Dear Guyz:) I have 2 different input files like this. I would like to pick the values or letters from the inputfile2 based on inputfile1 keys (A,F,N,X,Z). I have done similar task by using awk but in that case the inputfiles are similar like in inputfile2 (all keys in 1st column and values in... (16 Replies)
Discussion started by: repinementer
16 Replies
Login or Register to Ask a Question