How can I count how many characters appear within a file, minus those from a specific list. Here is an example file:
你好吗?
我很好,你呢?
我也很好。
I want to exclude any occurrences of ?
, ,
, and 。
from the count. The output would look like this:
3
5
4
How can I count how many characters appear within a file, minus those from a specific list. Here is an example file:
你好吗?
我很好,你呢?
我也很好。
I want to exclude any occurrences of ?
, ,
, and 。
from the count. The output would look like this:
3
5
4
A pure bash solution:
while IFS= read -r l; do
l=${l//[?,。]/}
echo "${#l}"
done < file
Try
sed 's/[,。?]//g' file | perl -C -nle 'print length'
The sed
part removes unwanted characters, and the perl
part counts the remaining characters.
One way is to remove those characters from the stream and then use wc -m
. Here is an example that uses perl to remove the characters:
perl -pe 's/(\?|,|,|。)//g' file.txt | \
while read -r line; do
printf "$line" | wc -m ;
done
A simple solution, approached to this one, but using awk
:
sed 's/[?,。]//g' file | awk '{ print length($0) }'