Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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.

share|improve this question

8 Answers 8

awk -F, '{sum+=length($3)}; END {print +sum}' file
share|improve this answer
3  
Amen; awk was designed for processing column based files, line-by-line. The problem is perfectly suited for the tool. –  Ray May 7 at 13:34
    
What is the purpose of + in {print +sum} ? {print sum} works just as well. –  spuder May 7 at 16:18
3  
@spuder, that's to print 0 instead of an empty line when the input file is empty. –  Stéphane Chazelas May 7 at 17:09
2  
@Ray, on the other hand, the task can be achieved by having 3 basic utilities (each one of them being a fraction of the size of awk) cooperating to the case (working concurrently) in typical Unix spirit. You may notice how the cut+tr+wc one is 5 types as fast as this awk one itself 5 times as fast as the perl one. (at least on my system, in a UTF8 locale, tried on a 100MB file). –  Stéphane Chazelas May 8 at 6:00
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

)

share|improve this answer
    
But he specifies 'I am not able to use wc command to get output!' –  mikeserv May 7 at 13:37
3  
@mikeserv, which I interpret as I wasn't able to get wc to give me the character count which is why I show how to use wc in this context. –  Stéphane Chazelas May 7 at 13:40
    
Oh.... That is a very valid interpretation which never at all occurred to me... –  mikeserv May 7 at 13:41

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
share|improve this answer
    
Note that it returns a byte count, not necessarily a character count. –  Stéphane Chazelas May 7 at 12:36
    
@StephaneChazelas: length() return the logical characters count, not physical bytes according to perldoc. –  Gnouc May 7 at 12:50
    
But you need -Mopen=:locale for perl to use the user/system's definition of what a character is, otherwise it assumes characters are bytes. Try on a a,1,españa,2 input in a UTF-8 locale (the default on most systems). –  Stéphane Chazelas May 7 at 13:05
    
@StephaneChazelas: Oh, updated my answer. Thanks for good point! –  Gnouc May 7 at 13:45
cut -d, -f3 <<\DATA | grep -o . | grep -c .
abd,123,egypt,78
cde,456,england,45
DATA

#OUTPUT
12
share|improve this answer

You could also use

awk -F, '{printf "%s", $3}' file | wc -m
share|improve this answer

In Perl:

perl -F, -Mopen=:locale -lane 'print length $F[2]' your_file
share|improve this answer
    
@StephaneChazelas Added, thanks. –  Joseph R. May 7 at 13:47

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.

share|improve this answer

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
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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