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
cluster-env,"manage_dirs_on_root","true"
cluster-env,"one_dir_per_partition","false"
cluster-env,override_uid","true"
cluster-env,recovery_enabled","false"
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.