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 Sep 25 '19 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 Sep 27 '19 at 13:45
  • the field delimiter is comma ',' . – Krishna Manchodu Sep 27 '19 at 13:58
1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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