parsed.org

Tips by tag: sort

Display files by size by http://felicity.me.uk/ on Apr 28, 2008 02:08 PM

For example, to display the 20 largest files owned by joe:

find / -printf "%k\t%p\n" -user joe | sort -n | tail -20
commandsfilesfindpermissionsprintfsorttailusers
Showing Duplicated Lines in a File by cygnus on Feb 15, 2005 04:09 PM

You can use this command to show duplicate lines in a file:

$ uniq -d MYFILE
commandsduplicatesfiltershellsortuniq
Standalone Procmail by xinu on Sep 10, 2005 12:10 AM

You have a mailbox you need to sort. You can't have it come back through your MTA, of course. Use this script to push it through the procmail filter of your choice:

#!/bin/sh

ORGMAIL=/var/spool/mail/$LOGNAME

if cd $HOME &&
  test -s $ORGMAIL &&
  lockfile -r0 -l3600 .newmail.lock 2>/dev/null
then
  trap "rm -f .newmail.lock" 1 2 3 15
  umask 077
  lockfile -l3600 -ml
  cat $ORGMAIL >>.newmail &&
  cat /dev/null >$ORGMAIL
  lockfile -mu
  formail -s procmail <.newmail &&
  rm -f .newmail
  rm -f .newmail.lock
fi
exit 0
commandsmailboxmtaprocmailscriptsshellsort
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
Xargs House-Cleaning by xinu on Jan 13, 2005 08:29 AM

If you have a bunch of files in your home directory and you want to push them into ~/sort, create the directory and then do the following:

$ find . -type f -maxdepth 1 ! -name ".?*" | xargs -I '{}' mv '{}' sort

Note: GNU xargs uses -i. BSD versions use -I.

bsdcommandsdirectoryfindgnushellsortxargs
RSS