Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select unique or distinct values from a list in UNIX shell script

I have a ksh script that returns a long list of values, newline separated, and I want to see only the unique/distinct values. It is possible to do this?

For example, say my output is file suffixes in a directory:

tar gz java gz java tar class class 

I want to see a list like:

tar gz java class 
like image 862
brabster Avatar asked Mar 06 '09 10:03

brabster


People also ask

How do you get unique values in Unix?

To find unique occurrences where the lines are not adjacent a file needs to be sorted before passing to uniq . uniq will operate as expected on the following file that is named authors. txt . As duplicates are adjacent uniq will return unique occurrences and send the result to standard output.

Which command will result unique list of numbers in Unix?

The uniq command can count and print the number of repeated lines. Just like duplicate lines, we can filter unique lines (non-duplicate lines) as well and can also ignore case sensitivity.

What is $@ in bash script?

bash [filename] runs the commands saved in a file. $@ refers to all of a shell script's command-line arguments. $1 , $2 , etc., refer to the first command-line argument, the second command-line argument, etc. Place variables in quotes if the values might have spaces in them.


1 Answers

You might want to look at the uniq and sort applications.

 ./yourscript.ksh | sort | uniq 

(FYI, yes, the sort is necessary in this command line, uniq only strips duplicate lines that are immediately after each other)

EDIT:

Contrary to what has been posted by Aaron Digulla in relation to uniq's commandline options:

Given the following input:

 class jar jar jar bin bin java 

uniq will output all lines exactly once:

 class jar bin java 

uniq -d will output all lines that appear more than once, and it will print them once:

 jar bin 

uniq -u will output all lines that appear exactly once, and it will print them once:

 class java 
like image 61
Matthew Scharley Avatar answered Oct 16 '22 01:10

Matthew Scharley