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.

"AGNOLI Valerio","ITA","AST"

In this example, the 2nd column says "ITA". I expect there are about 100 or so different nationalities listed in this file. I want to know exactly how many different nationalities there are.

share|improve this question

2 Answers 2

cut -d ',' -f 2 filename | sort -u | wc -l

Basically, I am specifying the , as the delimiter in the cut command and extracting the values in the second column using -f flag. Now, I sort them using sort and the -u flag makes the command to list only unique values. Finally, I have the wc -l command to get the count of unique countries in the second column.

Testing

cat filename
jill,us,123
jack,us,345
jill,en,234
mark,en,432
kate,us,354
kane,ru,435

Now, after issuing the command, I get the output as,

cut -d ',' -f 2 filename | sort -u | wc -l
3
share|improve this answer
    
maybe add | wc -l at the end? I think the OP wanted to count different nationalities. –  Simply_Me Aug 13 at 16:21
    
@Simply_Me, thanks. I added the wc -l to the end. –  Ramesh Aug 13 at 16:22
1  
sort | uniq may be shorten to sort -u (also more efficient). See also LC_ALL=C sort -u to report unique lines, as opposed to lines that sort the same (won't make a difference if that's only ASCII letters though). –  Stéphane Chazelas Aug 13 at 16:25
    
@StéphaneChazelas, thanks. I have made the changes as you suggested. :) –  Ramesh Aug 13 at 16:25
1  
you don't need cut, sort can sort columns: sort -u -t, -k2 –  richard Aug 13 at 17:11

You can use awk which will do it more efficiently and do the count for you instead of invoking different binaries and forking a lot of processes.

awk -F, '{a[$2]++} END{ for (i in a) print i, a[i]}' filename.csv
share|improve this answer
    
Note: this will fail if there are , in any of the quoted fields. –  richard Aug 13 at 17:05
    
@richard based on "AGNOLI Valerio","ITA","AST" I'm not sure how it will fail. –  val0x00ff Aug 13 at 17:25
    
I assume that the 2 examples are just example, else we could tell you the answer is 1. Can I also assume that there are no commas in the fields of other records. –  richard Aug 13 at 17:42

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.