I have a CSV file like this:
abd,123,egypt,78
cde,456,england,45
How can I get the character count of only the 3rd column words?
I can't figure out how to get wc to do this.
I have a CSV file like this:
abd,123,egypt,78
cde,456,england,45
How can I get the character count of only the 3rd column words?
I can't figure out how to get wc to do this.
awk -F, '{sum+=length($3)}; END {print +sum}' file
cut -d, -f3 | tr -d '\n' | wc -m
(remember that wc -c counts bytes, not characters:
$ echo a,1,españa,2 | cut -d, -f3 | tr -d '\n' | wc -c
7
$ echo a,1,españa,2 | cut -d, -f3 | tr -d '\n' | wc -m
6
)
A perl solution:
perl -Mopen=:locale -F, -anle '$sum += length($F[2]); END{print $sum}' file
or a shorter version:
perl -Mopen=:locale -F, -anle '$sum += length($F[2])}{print $sum' file
cut -d, -f3 <<\DATA | grep -o . | grep -c .
abd,123,egypt,78
cde,456,england,45
DATA
#OUTPUT
12
You could also use
awk -F, '{printf "%s", $3}' file | wc -m
With your sample file like so:
$ cat sample.txt
abd,123,egypt,78
cde,456,england,45
$ awk -F, '{print $3}' sample.txt | while read i; do echo "$i" | \
tr -d '\n' | wc -m; done
5
7
Working with wc to get each line's count can be tricky. You have to call it for each string from column 3 individually which makes it a bit tricky to do what you want. You have to look through each row of your CSV, extract column 3 and then present it to wc to get the character count.
Using sed and awk
sed 's/.*,.*,\(.*\),.*/\1/g' file | awk -v FS="" '{print NF;}'
Example:
$ (echo abd,123,egypt,78; echo cde,456,england,45;) | sed 's/.*,.*,\(.*\),.*/\1/g' | awk -v FS="" '{print NF;}'
5
7
Two awk's
awk -F, '{print $3}' file | awk -v FS="" '{print NF;}'
Example:
$ (echo abd,123,egypt,78; echo cde,456,england,45;) | awk -F, '{print $3}'| awk -v FS="" '{print NF;}'
5
7