AWK:在 csv 中间插入列的副本

AWK:在 csv 中间插入列的副本

示例 csv:

AAA, BBB, CCC, DDD, EEE, FFF, GGG, HHH

现在我不需要复制第 2 列 (BBB) 并将其添加到第 3 列前面,因此文件如下所示:

AAA, BBB, BBB, CCC, DDD, EEE, FFF, GGG, HHH

答案1

$ cat test.txt
AAA, BBB, CCC, DDD, EEE, FFF, GGG, HHH
AAA, BBB, CCC, DDD, EEE, FFF, GGG, HHH
AAA, BBB, CCC, DDD, EEE, FFF, GGG, HHH

$ awk -F, '{$2=$2","$2}1' OFS=, test.txt
AAA, BBB, BBB, CCC, DDD, EEE, FFF, GGG, HHH
AAA, BBB, BBB, CCC, DDD, EEE, FFF, GGG, HHH
AAA, BBB, BBB, CCC, DDD, EEE, FFF, GGG, HHH

答案2

awk '{print $1,$2,$2,$3,$4,$5,$6,$7,$8}' file.csv

例子:

 ➤ echo "AAA, BBB, CCC, DDD, EEE, FFF, GGG, HHH" | awk '{print $1,$2,$2,$3,$4,$5,$6,$7,$8}'
 AAA, BBB, BBB, CCC, DDD, EEE, FFF, GGG, HHH

相关内容