Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

awk count number of fields and add accordingly

I have the following file.csv

111111 | 111111 | 22222 | 44444 | 4445454 | 67554333 | 

I can count the number of fields using the following

awk -F '|' '{print NF}' file.csv

in my database schema I have 33 fields, however some of the lines in my csv file has less than 33 fields therefore when I import the file, it complains about miss match.

using awk how can I go about adding NULL fields spreader by | in order to full up 33 rows

your help is highly appreciated.

Thank you

like image 622
Deano Avatar asked Mar 23 '12 14:03

Deano


2 Answers

To add empty fields at the end of line:

awk -F'|' -v OFS='|' '{for(i=NF+1;i<=33;i++)$i=""}1' file.csv
like image 141
kev Avatar answered Oct 21 '22 21:10

kev


This might work for you:

sed ':a;s/|/&/33;t;s/$/|/;ta' file.csv
like image 36
potong Avatar answered Oct 21 '22 20:10

potong