How to count number of double quotes in bash

Need a little help.

I have just a simple string with a lot double quotes in it. I need to be able to parse through this string, and know how many double quotes I have, and where I am, so I can key off every 9th double quote. For example (coding is not complete):

#!/bin/bash
count=0
CSV=$(long-string-with-double-quotes)
IFS="\""
for v in $CSV
    **count double quotes here**
    if [ $count -eq 9 ];then
         echo "Count is 9"
         count=0
    else
         printf "%s" $CSV
         ((count++))
    fi

******
The problem is, I have no clue on how to count the number of double quotes in this string. I just need some syntax help if you can spare it. Thanks!

Hi,

Try with Perl. Here an example:

$ echo '"one","two","three"' | perl -ne 'printf "%d\n", tr/"/"/'
6

Regards,
Birei

1 Like

Thanks, that helps.

With pure bash you can do it like this:

$ string='"one", "two", "three"'
$ QT=${string//[^\"]/}
$ echo ${#QT}
6