行值添加到列

时间:2016-12-22 13:42:00

标签: bash shell row multiple-columns sunos

实际上我是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

能做到吗?

2 个答案:

答案 0 :(得分:1)

你可以试试这个;

EditingControlShowing

测试;

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

答案 1 :(得分:0)

请你试着跟我一起,让我知道这是否有帮助。

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

我希望这会对你有所帮助。