For example, we want to count all quote (") characters; we just worry if files have more quotes than it should.

For example:


expected results:



You can combine tr (translate or delete characters) with wc (count words, lines, characters):

tr -cd '"' < yourfile.cfg | wc -c

(-delete all characters in the complement of ", and then count the characters.)

| improve this answer | |

grep approach:

grep -o '"' file | wc -l
  • -o - output only matched substrings

Or with single gawk:

awk -v RS='' -v FPAT='"' '{print NF}' file
  • RS='' - empty record separator (instead of newline)

  • FPAT='"' - pattern defining field value

| improve this answer | |

If two lines in the file has an odd number of double quotes, the total sum of double quotes will be even, and you will not detect unbalanced quotes (this is what I presume you'd like to actually do, but I might be wrong).

This awk script reports any line in the input line that has an odd number of quotes:

awk -F'"' 'NF % 2 == 0 { printf("Line %d has odd quoting: %s\n", NR, $0) }'

We set the field separator (FS) to " with -F'"' which means that if a line has an even number of fields it has odd quotes. NF is the number of fields in the recent record, and NR is the ordinal number of the current record ("the line number").

Given the following input:

$ cat file

we get

$ awk -F'"' 'NF % 2 == 0 { printf("Line %d has odd quoting: %s\n", NR, $0) }' file
Line 3 has odd quoting: cluster-env,override_uid","true"
Line 4 has odd quoting: cluster-env,recovery_enabled","false"

Something like

$ grep -o '"' | wc -l

would return "14" for this file.

| improve this answer | |

Another single gawk approach:

awk -v RS=\" 'END{print NR-1}'
| improve this answer | |

Pure BASH:

var="$(< file.txt)"
echo ${#tmp}
| improve this answer | |
  • Is tmp an array? If yes, tmp is an array of what? – Tim Aug 23 '17 at 0:14
  • @Tim, no. tmp in this snippet is a normal shell variable. And I'm downvoting this answer because this counts the number of times a character appears in a variable (var) rather than in a file as specified in the question. – Wildcard Aug 23 '17 at 3:33


grep -0 '"' File -c

however, this will not work if two or more char in the same line. They will be counted as one char

| improve this answer | |
  • 1
    Welcome on U&L! This doesn't seem to actually answer the question, since it will count lines instead of characters. Writing effective answers is strongly encouraged on U&L - see Answering in the help center. You might want to improve this one. – fra-san Dec 1 '18 at 20:12

grep -oF ' " ' file | wc -l

-F stands for fixed string

| improve this answer | |

Eccentric double GNU grep method:

grep -o \" file | grep -c .
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.