Count unique values ​​in column with shell script

I have a tab delimited file with 5 columns and you need to get a count of the number of unique rows from column 2. I usually do this using Perl / Python, but I have to use a wrapper for this.

In the past, I have successfully used the * nix uniq function connected to wc, but it looks like I have to use awk here.

Any advice would be greatly appreciated. (I previously asked a similar question about column checks using awk, but this is a bit different, and I wanted to separate it, so if anyone has this question in the future, it will be here)

Many thanks! Lilly

+5
source share
3 answers

awk.

$ cut -f2 file.txt | sort | uniq | wc -l

.

, cut , 2 . sort uniq, . , , .

+16

$ cut -f2 file.txt | sort -u | wc -l

, uniq ( ).

, Solaris:

uniq , , . .

, .

+5
awk '{if($0~/Not Running/)a++;else if($0~/Running/)b++}END{print a,b}' temp
0

All Articles