I have a scenario where i want to get count of all values row by row and store it to dynamic array
Data in file :
"A","B","C","B"
"P","W","R","S"
"E","U","C","S"
"Y","F","C"
first row as : 4 -> values
second row as : 4 -> values
third row as : 4 -> values
fourth row as : 3 -> values
Expected Output : store to array : array_list=(4,4,4,3)
written a script but not working
array_list=()
while read -r line
do
var_comma_count=`echo "$line" | tr -cd , | wc -c`
array_list=+($( var_comma_count))
done < demo.txt
when i print array it should give me all values : echo "{array_list[@]}"
Note : The file might contain empty lines at last which should not be read
when i count file it gave me count : 5 , it should have ignored last line which is empty
where as when i use awk it give me proper count : awk '{print NF}' demo.txt -> 4
I know processing file using while loop is not a best practise , but any better solution will be appreciated
array_list=($(awk '{print NF}' demo.txt))?store it to dynamic arrayit's just an "array", it's not called "dynamic" I mean.