3

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.

2 Answers 2

4
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
8
  • maybe add | wc -l at the end? I think the OP wanted to count different nationalities.
    – Simply_Me
    Aug 13, 2014 at 16:21
  • @Simply_Me, thanks. I added the wc -l to the end.
    – Ramesh
    Aug 13, 2014 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). Aug 13, 2014 at 16:25
  • @StéphaneChazelas, thanks. I have made the changes as you suggested. :)
    – Ramesh
    Aug 13, 2014 at 16:25
  • 1
    you don't need cut, sort can sort columns: sort -u -t, -k2 Aug 13, 2014 at 17:11
2

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
3
  • Note: this will fail if there are , in any of the quoted fields. Aug 13, 2014 at 17:05
  • @richard based on "AGNOLI Valerio","ITA","AST" I'm not sure how it will fail. Aug 13, 2014 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. Aug 13, 2014 at 17:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .