在unix中打印csv文件

时间:2013-12-26 07:14:36

标签: unix csv awk

我正在尝试仅打印来自csv file1.csv的那些记录,其中col $1不是空白。符合此条件的记录将被复制到另一个csv file2.csv

awk -F "," '{$1 !=""; OFS=","} {print $1,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14,$15,$16,$17,$18,$19,$20}' file1.csv > file2.csv

但它只重新生成整个file1.csv,只有要打印的列

我在这里错过了if表达式吗?

3 个答案:

答案 0 :(得分:0)

试试这个......

awk 'BEGIN {FS=OFS=","} { if($1 != "") print }' file1.csv > file2.csv

答案 1 :(得分:0)

你已将花括号放在{$1 !=""; OFS=","}的错误位置,它应该更像$1 !="" {print ... },所以你可以试试这个:

awk -F, '$1 {print $1,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14,$15,$16,$17,$18,$19,$20}' OFS=, file1.csv > file2.csv

答案 2 :(得分:0)

试试这个

awk -F "," '$1 !=""{OFS=","; print $1,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14,$15,$16,$17,$18,$19,$20}' file1.csv > file2.csv

仅在第一个字段中打印具有非空值的行。