finding unique values in a data file

Viewed 97531

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)

8 Answers

IMHO Michał Šrajer got the best answer but a filename needed after grep name1 And i've got this fancy solution using indexed array

user=name1

IFSOLD=$IFS; IFS=$'\n'; test=( $(grep $user test) ); IFS=$IFSOLD
declare -A index
for item in "${test[@]}"; {
    sub=( $item )
    name=${sub[3]}
    index[$name]=$item
}

for item in "${index[@]}"; { echo $item; }

The following command worked for me.

sudo cat AirtelFeb.txt | awk '{print $3}' | sort -u

Here it prints the 3rd column with unique values.

I think you meant fourth column. You can try using 'cat Filename.txt | awk '{print $4}' | sort | uniq'

Related