1

I'm a Unix shell script newbie. I know several different way to find duplicates. But can't find a simple way to remove duplicates while maintaining original order (since using sort -u loses original order).

Example: script called dedupe.sh

sample run:

dedupe.sh

cat dog cat bird fish bear dog

results in: cat dog bird fish bear

2
  • This isn't the sort of thing shell scripting is designed for. Which shell are you using? You should probably add a perl tag to this, someone will pop by with a solution in perl. Commented Mar 18, 2013 at 0:48
  • Is this in a file? Use uniq if it is. Commented Mar 18, 2013 at 0:50

3 Answers 3

2

Using :

$ printf '%s\n' cat dog cat bird fish bear dog | awk '!arr[$1]++'
cat
dog
bird
fish
bear

or

$ echo 'cat dog cat bird fish bear dog' | awk '!arr[$1]++' RS=" "

or

$ printf '%s\n' cat dog cat bird fish bear dog | sort -u

If it works in a , it will works in a script =)

Sign up to request clarification or add additional context in comments.

Comments

1

Did you say Perl?

perl -e 'while($_=shift@ARGV){$seen{$_}++||print}print"\n" ' \
cat dog cat bird fish bear dog

Equivalently, dedupe.pl contains:

#!/usr/bin/perl
while ($w = shift @ARGV) {
    $seen{$w}++ || print "$w";
}
print "\n";

Now chmod u+x dedupe.pl and:

./dedupe.pl cat dog cat bird fish bear dog

Either way, output is as desired.

cat dog bird fish bear 

Comments

0

Ahh perl... the write-only language. :)

As long as you're calling out to another scripting language, might as well consider something readable. :)

#!/usr/bin/env ruby

puts ARGV.uniq.join(' ')

which means:

puts = "print whatever comes next"
ARGV = "input argument array"
uniq = "array method to perform the behavior you're looking for and remove duplicates"
join(' ') = "join with spaces instead of default of newline. Not necessarily needed if you're piping to something else"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.