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

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


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:

classjarjarjarbinbinjava

uniq will output all lines exactly once:

classjarbinjava

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

jarbin

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

classjava


./script.sh | sort -u

This is the same as monoxide's answer, but a bit more concise.


With zsh you can do this:

% cat infile tarmore than one wordgzjavagzjavatarclassclasszsh-5.0.0[t]% print -l "${(fu)$(<infile)}"tarmore than one wordgzjavaclass

Or you can use AWK:

% awk '!_[$0]++' infile    tarmore than one wordgzjavaclass