0

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

7
  • array_list=($(awk '{print NF}' demo.txt))? store it to dynamic array it's just an "array", it's not called "dynamic" I mean. Commented Jun 6, 2021 at 10:40
  • Can your quoted fields contains commas or newlines? Commented Jun 6, 2021 at 12:02
  • Also, you've been told before that you should copy/paste your scripts into shellcheck.net before asking questions about them, please do that and then edit your question to replace your current script with the fixed result. Commented Jun 6, 2021 at 12:04
  • @EdMorton Sure next time will check code with shell check and post question Commented Jun 6, 2021 at 12:07
  • 1
    No need to wait for next time, you can do it right now this time, it'll take you about 2 minutes at most. Commented Jun 6, 2021 at 12:09

1 Answer 1

2

Perhaps this might be easier using awk, set the FS to a comma and check if the number of fields is larger than 0:

#!/bin/bash

array_list=($(awk -v FS=, 'NF>0 {print NF}' demo.txt))
echo "${array_list[@]}"

Output

4 4 4 3

The awk command explained:

awk -v FS=, '    # Start awk, set the Field Separator (FS) to a comma
NF>0 {print NF}  # If the Number of Fields (NF) is greater than 0, print the NF
' demo.txt       # Close awk and set demo.txt as the input file

Another option could be first matching the format of the whole line. If it matches, there is at least a single occurrence.

Then split the line on a comma.

array_list=($(awk '/^"[A-Z]"(,"[A-Z]")*$/{print(split($0,a,","));}' demo.txt))
echo "${array_list[@]}"

Output

4 4 4 3

The awk command explained:

awk '/^"[A-Z]"(,"[A-Z]")*$/{   # Regex pattern for the whole line, match a single char A-Z between " and optionally repeat preceded by a comma
  print(split($0,a,","));      # Split the whole line `$0` on a comma and print the number of parts
}
' demo.txt
Sign up to request clarification or add additional context in comments.

3 Comments

can you explain the code in detail will be helpful for others too
is it possible to use this @ instead of '*' in array_list [ * ]
@codeholic24 Yes, I have updated the answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.