行值添加到列

Row value adding to column

实际上,我对 shell 脚本编写还很陌生。假设我有 2 列,用逗号分隔

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

我还有一排。此行值可能会更改。像下面的行有 5 个数字,用逗号分隔。它可能有超过 5 个数字(例如:1,5,3,7,8,9,10,...)

41,42,43,44,45

我想从下面的行中添加值

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

可以吗?

你可以试试这个;

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

测试;

$ ./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

能否请您也尝试关注一下,如果有帮助请告诉我。

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

希望对你有所帮助。