Awk脚本处理CSV合并数组列时额外输出原始行问题求助
问题原因
你的awk脚本末尾存在多余的独立关键字FILENAME。
awk的语法规则为「匹配模式 { 执行动作 }」,如果只写了匹配模式没有指定对应的动作,默认会执行打印当前行的操作。因此每一行完成你编写的格式转换逻辑后,都会再触发一次默认打印逻辑,输出原始未处理的行,最终出现重复内容。
修复方案
直接删除脚本末尾孤零零的FILENAME字符串即可,修改后的完整脚本如下:
#!/bin/awk -f BEGIN { getline; print $0; num_cols = NF; #printf("num_cols: %s, array_col: %s\n\n", num_cols, array_col); } NR>1 { total_fields = NF; # fields_before_array = (array_col - 1) # fields_before_array + array_length + fields_after_array = NF # fields_before_array + fields_after_array + 1 = num_cols # array_length - 1 = total_fields - num_cols # array_length = total_fields - num_cols + 1 # fields_after_array = total_fields - array_length - fields_before_array # = total_fields - (total_fields - num_cols + 1) - (array_col - 1) # = num_cols - array_col fields_before_array = (array_col - 1); array_length = total_fields - num_cols + 1; fields_after_array = num_cols - array_col; first_array_position = array_col; last_array_position = array_col + array_length-1; #printf("array_col: %s, fields_before_array: %s, array_length: %s, fields_after_array: %s, total_fields: %s, num_cols: %s", array_col, fields_before_array, array_length, fields_after_array, total_fields, num_cols) ### loop through fields before array column ### remove whitespace, and print surround with "" for (i=1; i<array_col; i++) { gsub(/ /,"",$i); printf("\"%s\",", $i); } ### Collect array surrounded by "" array_data = ""; ### Loop through array for (i=array_col ; i<array_col+array_length-1 ; i++) { gsub(/ /, "", $i); array_data = array_data $i ","; } ### collect last array element with no trailing , array_data = array_data $i ### print array surrounded by quotes printf("\"%s\",", array_data); ### loop through remaining fields, remove whitespace, surround with "" for (i=last_array_position+1 ; i<total_fields ; i++) { gsub(/ /,"",$i); printf("\"%s\",", $i); } ### finish line with \n printf("\"%s\"\n", $total_fields); }
修改后重新运行脚本即可得到你预期的输出结果。
内容的提问来源于stack exchange,提问作者abalter
相关产品推荐
相关产品推荐

