Row value adding to column

142 views Asked by At

Actually I am pretty new in shell scripting. suppose i have a 2 columns separated by comma

OFFER_DELETE_SC_LEVEL,146
OFFER_DELETE_SC_LEVEL,25
OFFER_DELETE_SC_LEVEL,18
OFFER_DELETE_SC_LEVEL,17
OFFER_DELETE_SC_LEVEL,27

I have another row. This row value may change. Like below the row have 5 numbers separated by comma. It may have more then 5 numbers(example: 1,5,3,7,8,9,10,...)

41,42,43,44,45

i want to add value from the row like below

OFFER_DELETE_SC_LEVEL,146,41
OFFER_DELETE_SC_LEVEL,25,41
OFFER_DELETE_SC_LEVEL,18,41
OFFER_DELETE_SC_LEVEL,17,41
OFFER_DELETE_SC_LEVEL,27,41
OFFER_DELETE_SC_LEVEL,146,42
OFFER_DELETE_SC_LEVEL,25,42
OFFER_DELETE_SC_LEVEL,18,42
OFFER_DELETE_SC_LEVEL,17,42
OFFER_DELETE_SC_LEVEL,27,42
OFFER_DELETE_SC_LEVEL,146,43
OFFER_DELETE_SC_LEVEL,25,44
OFFER_DELETE_SC_LEVEL,18,44
OFFER_DELETE_SC_LEVEL,17,44
OFFER_DELETE_SC_LEVEL,27,44
OFFER_DELETE_SC_LEVEL,146,45
OFFER_DELETE_SC_LEVEL,25,45
OFFER_DELETE_SC_LEVEL,18,45
OFFER_DELETE_SC_LEVEL,17,45
OFFER_DELETE_SC_LEVEL,27,45

can it be done?

2

There are 2 answers

6
Mustafa DOGRU On BEST ANSWER

you can try this;

#!/bin/bash
 for var in $(sed 's/,/\n/g' file2); do
        awk -v var=$var '{print $0"," var} ' file1
 done

Test;

$ ./test.sh 
OFFER_DELETE_SC_LEVEL,146,41
OFFER_DELETE_SC_LEVEL,25,41
OFFER_DELETE_SC_LEVEL,18,41
OFFER_DELETE_SC_LEVEL,17,41
OFFER_DELETE_SC_LEVEL,27,41
OFFER_DELETE_SC_LEVEL,146,42
OFFER_DELETE_SC_LEVEL,25,42
OFFER_DELETE_SC_LEVEL,18,42
OFFER_DELETE_SC_LEVEL,17,42
OFFER_DELETE_SC_LEVEL,27,42
OFFER_DELETE_SC_LEVEL,146,43
OFFER_DELETE_SC_LEVEL,25,43
OFFER_DELETE_SC_LEVEL,18,43
OFFER_DELETE_SC_LEVEL,17,43
OFFER_DELETE_SC_LEVEL,27,43
OFFER_DELETE_SC_LEVEL,146,44
OFFER_DELETE_SC_LEVEL,25,44
OFFER_DELETE_SC_LEVEL,18,44
OFFER_DELETE_SC_LEVEL,17,44
OFFER_DELETE_SC_LEVEL,27,44
OFFER_DELETE_SC_LEVEL,146,45
OFFER_DELETE_SC_LEVEL,25,45
OFFER_DELETE_SC_LEVEL,18,45
OFFER_DELETE_SC_LEVEL,17,45
OFFER_DELETE_SC_LEVEL,27,45
1
RavinderSingh13 On

Could you please try following also and let me know if this helps.

awk 'FNR==NR{A[++num]=$0;next} {for(j=1;j<=NF;j++){for(i=1;i<=num;i++){print A[i]","$j}}}' file1 FS="," file2

I hope this helps you.