[英]awk insert in nth row if pattern not match in nth column of CSV
I have CSV file as below , I need to compare 2nd column and insert the data in nth row . 我有以下CSV文件,我需要比较第二列并将数据插入第n行。
CSV: 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
Input: 输入:
11-AUG-17,3,0,0
11-AUG-17,8,0,0
Desire Output: 需求输出:
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
I have tried the below awk
command but it is inserting in all field as below 我已经尝试了以下
awk
命令,但它正在如下所示的所有字段中插入
awk -F"," 'OFS="," $2!=3{print $1,3,0,0}1;$2!=8{print $1,12,0,0};' result1.csv >result2.csv
output of above command: 上面命令的输出:
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
Thanks in advance for support 预先感谢您的支持
Now, according to your words " no need to maintain order/sequence .just insertion is required in any row of CSV ", simple cat
command would be enough: 现在,按照您的话“ 不需要维护顺序/顺序。只需在CSV的任何行中插入 ”,简单的
cat
命令就足够了:
cat input.csv >> first.csv
Final first.csv
contents: 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.