如果模式在CSV的第n列中不匹配,则awk插入第n行

时间:2017-08-29 07:05:27

标签: awk

我有以下CSV文件,我需要比较第二列并在第n行插入数据。

CSV:

11-AUG-17,2,85,18120.4
11-AUG-17,4,112,2920
11-AUG-17,5,23,4181.92
11-AUG-17,6,284,74767.86

输入:

11-AUG-17,3,0,0
11-AUG-17,8,0,0

欲望输出:

11-AUG-17,2,85,18120.4
11-AUG-17,4,112,2920
11-AUG-17,5,23,4181.92
11-AUG-17,8,0,0
11-AUG-17,6,284,74767.86
11-AUG-17,3,0,0

我已尝试过以下awk命令,但它会插入所有字段,如下所示

awk -F"," 'OFS="," $2!=3{print $1,3,0,0}1;$2!=8{print $1,12,0,0};' result1.csv >result2.csv

输出上述命令:

11-AUG-17,2,85,18120.4
11-AUG-17,3,0,0
11-AUG-17,8,0,0
11-AUG-17,4,112,2920
11-AUG-17,3,0,0
11-AUG-17,8,0,0
11-AUG-17,5,23,4181.92
11-AUG-17,3,0,0
11-AUG-17,8,0,0
11-AUG-17,6,284,74767.86
11-AUG-17,3,0,0
11-AUG-17,8,0,0

提前致谢支持

1 个答案:

答案 0 :(得分:0)

现在,根据您的说法“ 无需维护订单/顺序。只需插入任何CSV行 ”,简单cat命令就足够了:

 cat input.csv >> first.csv

最终first.csv内容:

11-AUG-17,2,85,18120.4
11-AUG-17,4,112,2920
11-AUG-17,5,23,4181.92
11-AUG-17,6,284,74767.86
11-AUG-17,3,0,0
11-AUG-17,8,0,0