mariswamy kantha
mariswamy kantha

Reputation: 61

use uniq -d on a particular column?

Have a text file like this.

john,3
albert,4
tom,3
junior,5
max,6
tony,5

I'm trying to fetch records where column2 value is same. My desired output.

john,3
tom,3
junior,5
tony,5

I'm checking if we can use uniq -d on second column?

Upvotes: 3

Views: 3345

Answers (2)

Steve
Steve

Reputation: 54392

Here's one way using . It reads the input file twice, but avoids the need to sort:

awk -F, 'FNR==NR { a[$2]++; next } a[$2] > 1' file file

Results:

john,3
tom,3
junior,5
tony,5

Brief explanation:

FNR==NR is a common AWK idiom that is true for the first file in the arguments list. Here, column two is added to an array and incremented. On the second read of the file, we simply check if the value of column two is greater than one (the next keyword skips processing the rest of the code).

Upvotes: 4

dave
dave

Reputation: 11975

You can use uniq on fields (columns), but not easily in your case.

Uniq's -f and -s options filter by fields and characters respectively. However neither of these quite do what want.

-f divides fields by whitespace and you separate them with commas. -s skips a fixed number of characters and your names are of variable length.

Overall though, uniq is used to compress input by consolidating duplicates into unique lines. You are actually wishing to retain duplicates and eliminate singletons, which is the opposite of what uniq is used to do. It would appear you need a different approach.

Upvotes: 1

Related Questions