How do I get the total number of distinct values in a column in a CSV?

Username

I have a CSV file named test.csv. It looks like this:

1,Color
1,Width
2,Color
2,Height

I want to find out how many distinct values are in the first column. The shell script should return 2 in this case.

I tried running sort -u -t, -k2,2 test.csv, which I saw on another question, but it printed out far more info than I need.

How do I write a shell script that prints the number of distinct values in the first column of test.csv?

anubhava

Using awk you can do:

awk -F, '!seen[$1]++{c++} END{print c}' file

2

This awk command uses key $1, and stores them in an array seen. Value of which is incremented to 1 when a key is populated first time. Every time we get a unique key we increment count c and print it in the end.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

SQL: How would I get a total count for distinct values in a column?

From Dev

How do I get the distinct/unique values in a column in Excel?

From Dev

MySQL: How do I get the AVG value in one column for entries sharing distinct values in another column?

From Dev

How do I sum the total number of values against a given value in another column?

From Dev

How can I get the total number of rows in a CSV file with PHP?

From Dev

How do I count distinct combinations of column values?

From Dev

How do I calculate total number of unique values in a dataframe?

From Dev

How do I calculate total number of unique values in a dataframe?

From Dev

How to get the total count of rows AND the count based on the distinct values of another column in the table

From Dev

SQL on Spark: How do I get all values of DISTINCT?

From Dev

How do I get distinct count values of 2 columns?

From Dev

How do I get total number of objects in a variable only once?

From Dev

How do you get distinct values from dataTables and sum the total specific field using JS

From Dev

In Django, how could I in a single query get total row count based on distinct field values?

From Dev

How to get number of distinct values ? mysql

From Dev

In R: How do I get the column names of a CSV file as a list and values as a list of lists

From Dev

How can I insert values as a distinct column?

From Dev

How do I count the number of nonzero values in a given array column?

From Dev

How can i get CSV column values in php

From Dev

How Do I Sum the Total Of The Column Values If filtering the pending values in Angularjs?

From Dev

How do I sum distinct values?

From Dev

determining total number of times distinct values 0 or 1 or na in each column in a data frame in R

From Dev

How can i get the total of a column in mysql?

From Dev

How do I get the total number of records before I run limit when using Aggregation

From Dev

How do I summarize the total number of radio button selection values (Letters, and not numbers)?

From Dev

How do I get distinct grouped data?

From Dev

How do I get the averages of duplicate values in a postgresql column?

From Dev

How do i get column values of all checked rows?

From Dev

How do I get all values of a column in sqlite3?

Related Related

  1. 1

    SQL: How would I get a total count for distinct values in a column?

  2. 2

    How do I get the distinct/unique values in a column in Excel?

  3. 3

    MySQL: How do I get the AVG value in one column for entries sharing distinct values in another column?

  4. 4

    How do I sum the total number of values against a given value in another column?

  5. 5

    How can I get the total number of rows in a CSV file with PHP?

  6. 6

    How do I count distinct combinations of column values?

  7. 7

    How do I calculate total number of unique values in a dataframe?

  8. 8

    How do I calculate total number of unique values in a dataframe?

  9. 9

    How to get the total count of rows AND the count based on the distinct values of another column in the table

  10. 10

    SQL on Spark: How do I get all values of DISTINCT?

  11. 11

    How do I get distinct count values of 2 columns?

  12. 12

    How do I get total number of objects in a variable only once?

  13. 13

    How do you get distinct values from dataTables and sum the total specific field using JS

  14. 14

    In Django, how could I in a single query get total row count based on distinct field values?

  15. 15

    How to get number of distinct values ? mysql

  16. 16

    In R: How do I get the column names of a CSV file as a list and values as a list of lists

  17. 17

    How can I insert values as a distinct column?

  18. 18

    How do I count the number of nonzero values in a given array column?

  19. 19

    How can i get CSV column values in php

  20. 20

    How Do I Sum the Total Of The Column Values If filtering the pending values in Angularjs?

  21. 21

    How do I sum distinct values?

  22. 22

    determining total number of times distinct values 0 or 1 or na in each column in a data frame in R

  23. 23

    How can i get the total of a column in mysql?

  24. 24

    How do I get the total number of records before I run limit when using Aggregation

  25. 25

    How do I summarize the total number of radio button selection values (Letters, and not numbers)?

  26. 26

    How do I get distinct grouped data?

  27. 27

    How do I get the averages of duplicate values in a postgresql column?

  28. 28

    How do i get column values of all checked rows?

  29. 29

    How do I get all values of a column in sqlite3?

HotTag

Archive