parsed.org

Tips by tag: unique

Grep: Show only filenames by cygnus on Jun 03, 2005 11:12 AM

You can have grep show only the filenames that matched (instead of showing all lines in those files that matched) by using the -l switch:

$ grep -l SELECT *.sql
file1.sql
file2.sql

grep will only list unique filenames (i.e. a file with two matches will only be listed once).

commandsgrepshellunique
Unique Perl Sort by xinu on Dec 13, 2007 12:51 PM

You have an array of IP addresses but are only interested in the unique values. Since perl doesn't have a unique() function, we'll exploit the concept of unique keys in hashes:

#!/usr/bin/perl

my @ipAddresses = ('192.168.0.1', '192.168.0.27',
                   '192.168.0.1', '192.168.0.3');
print "Before: ", qq(@ipAddresses), "\n";

undef %saw;
@saw{@ipAddresses} = ();
@sorted = sort keys %saw;

print "After : ", qq(@sorted), "\n";

Resulting in:

Before: 192.168.0.1 192.168.0.27 192.168.0.1 192.168.0.3
After : 192.168.0.1 192.168.0.3 192.168.0.27
perlsortunique
RSS