我有一个文件,每一行都采用以下格式
KeyA=ValA1,ValA2,ValA3...ValAn
KeyB=ValB1,ValB2,ValB3....ValBn
我在该文件中有多行,每行的值数量不同。
我的任务是为每行附加Val Key
。预期样本输出:
ValA1 KeyA
ValA2 KeyA
ValA3 KeyA
ValB1 KeyB
ValB2 KeyB
ValB3 KeyB
我试过的是:
while read -r line; do
KEY=$(echo $line | cut -d '=' -f 1)
VALUES=$(echo $line | cut -d '=' -f 2)
for VAL in $VALUES;do
echo $VAL $KEY
done
done < file.txt
我能够达到预期的输出,但是应该在不使用for
循环的情况下完成此输出。
有人可以建议我其他解决方案吗?
分析解答