I have a CSV file of a table that has some entries that are blank. I want my code to look through all the entries for each row (except the first) and count the number of blank entries > store the blank entries per row and separate them with a comma into a file called temp_file > then I want the code to count the number of blank entries in that file and give me a value e.g. if it is,,, then the output should be 3.

This is currently what I have:

sed 1d ${FILENAME} | while read ln


echo $ln | tail -1 PMRExceptions.csv | tr "," "\n" | grep -nx '^$' | cut -d":" -f1

PMRNUM=echo $ln | awk -F"," '{print $2}'

PMRMGR=echo $ln | awk -F"," '{print $3}'

COUNT=less $TEMPFILE | wc -l 

this part over here is supposed to count the number of blank entries

while read line
columns=`head -1 $FILENAME | awk -F"," -v ln=$line '{print $ln}'`

The value that I get is 0, no matter what I change. I think that the entries are not being stored in the tempfile.


Using awk to count the number of comma-delimited fields on each line of input that are empty (excluding the header line):

awk -F ',' 'NR > 1 { c=0; for (i = 1; i <= NF; ++i) if ($i == "") ++c; print c }' file.csv

This assumes that no field contains embedded commas.


$ cat file.csv
$ awk -F ',' 'NR > 1 { c=0; for (i = 1; i <= NF; ++i) if ($i == "") ++c; print c }' file.csv


  • which part of the code will i need to replace this with? – infiniteoscillation Aug 10 '18 at 9:08
  • @infiniteoscillation All of it. The awk command I have in the answer reads the original CSV file and counts the number of empty fields on each line. – Kusalananda Aug 10 '18 at 9:10

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.