How to count the number of a specific character in each line?
You can do it with sed
and awk
:
$ sed 's/[^"]//g' dat | awk '{ print length }'
2
0
Where dat
is your example text, sed deletes (for each line) all non-"
characters and awk
prints for each line its size (i.e. length
is equivalent to length($0)
, where $0
denotes the current line).
For another character you just have to change the sed expression. For example for (
to:
's/[^(]//g'
Update: sed
is kind of overkill for the task - tr
is sufficient. An equivalent solution with tr
is:
$ tr -d -c '"\n' < dat | awk '{ print length; }'
Meaning that tr
deletes all characters which are not (-c
means complement) in the character set "\n
.
I would just use awk
awk -F\" '{print NF-1}' <fileName>
Here we set the field separator (with the -F flag) to be the character "
then all we do is print number of fields NF
- 1. The number of occurrences of the target character will be one less than the number of separated fields.
For funny characters that are interpreted by the shell you just need to make sure you escape them otherwise the command line will try and interpret them. So for both "
and )
you need to escape the field separator (with \
).
Using tr
ard wc
:
function countchar()
{
while IFS= read -r i; do printf "%s" "$i" | tr -dc "$1" | wc -m; done
}
Usage:
$ countchar '"' <file.txt #returns one count per line of file.txt
1
3
0
$ countchar ')' #will count parenthesis from stdin
$ countchar '0123456789' #will count numbers from stdin