0

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

do

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
do
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.

1 Answer 1

1

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.

Example:

$ cat file.csv
A,B,C
1,,0
all,bar,none
my,field,here
no,data,
$ awk -F ',' 'NR > 1 { c=0; for (i = 1; i <= NF; ++i) if ($i == "") ++c; print c }' file.csv
1
0
0
1

Related:

2
  • which part of the code will i need to replace this with? Commented Aug 10, 2018 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
    Commented Aug 10, 2018 at 9:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .