1

I have a CSV file, a.csv, which have 15000 rows and 16 columns. It have numbers, sentences, dates in different columns.

It looks like this:

A, B, C, D
a  b  c  d
e  f  g  h
e  i  j  k
a  m  n  k

How can I count the unique values in each column separately and print them at once?

I want an output that looks like this:

A 2
B 4
C 4
D 3

Any advice or help are highly appreciated. Thanks in advance.

3
  • May be you could transpose the text (rows→columns), then filtering lines would be easier.
    – kaliko
    Commented Sep 25, 2019 at 10:44
  • 1
    What is the field delimiter? The first row seems to use commas (and a space?) while the others just use two spaces...
    – Kusalananda
    Commented Sep 27, 2019 at 13:45
  • the field delimiter is comma ',' . Commented Sep 27, 2019 at 13:58

1 Answer 1

2

using xsv (https://github.com/BurntSushi/xsv) starting from

A,B,C,D
a,b,c,d
e,f,g,h
e,i,j,k
a,m,n,k

and running

xsv stats input.csv --everything | xsv select field,cardinality >output.txt

you will have

field,cardinality
A,2
B,4
C,4
D,3

You must log in to answer this question.

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