Count Columns and If less add new column

Hi All,

My Input.txt should have always 4 columns in some cases i may not get all the 4columns data.
My requirement is if columns as not equal to 4 then append new column delimiter.

Input.txt

A,1,2
B,1,2,3
C,1

Desired output should be in below format
Output.txt

A,1,2,
B,1,2,3
C,1,,

I had tried awk '$0=$0OFS NF-1' FS=, FILENAME

Thanks in advance

---------- Post updated at 11:48 AM ---------- Previous update was at 10:35 AM ----------

Got the solution

DEFAULT=4
j=1
for i in `awk -F"," '{print NF}' Final.txt`
do
final=`awk -F"," '{if( NR == '$j' ) print $0}' Final.txt`
while [ $i -lt $DEFAULT ]
do
final=$final","
i=$(($i + 1))
done
echo "$final" > DUMMY.txt
j=$(($j + 1))
done
mv DUMMY.txt Final.txt

awk -F, '{printf $0; for(i=4;i>NF;i--){printf ","} printf "\n"}' inputfile

Would this not be a bit simpler?:

$ awk -F, '{$4 = ($4 ? $4 : "")} 1' OFS=, file
A,1,2,
B,1,2,3
C,1,,
1 Like

Thanks