bashshellrowmultiple-columnssunos

Row value adding to column


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?


Solution

  • 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