Take the 2-minute tour ×

I can do this in python but I was wondering if I could do this in Linux

I have a file like this

name1 text text 123432re text
name2 text text 12344qp text
name3 text text 134234ts text

I want to find all the different types of values in the 3rd column by a particular username lets say name 1.

grep name1 filename gives me all the lines, but there must be some way to just list all the different type of values? (I don't want to display duplicate values for the same username)

share|improve this question

3 Answers 3

up vote 6 down vote accepted
grep name1 filename | cut -d ' ' -f 4 | sort -u

This will find all lines that have name1, then get just the fourth column of data and show only unique values.

share|improve this answer
If you have name1 in another field or a user named name1foo, the solution could give results you do not want. I would grep on "^name1 " (with a space after name1) to avoid that. –  jfgagne Aug 5 '11 at 10:30

You can let sort look only on 4-th key, and then ask only for records with unique keys:

grep name1 | sort -k4 -u
share|improve this answer

As an all-in-one awk solution:

awk '$1 == "name1" && ! seen[$1" "$4]++ {print $4}' filename
share|improve this answer

Your Answer


