weed-out-from a-list another-list

This week I wrote from scratch and then lost at least four times a script to clean up a list of elements from elements contained in another list --I had to make sure I do not accidentally block white-listed networks on Firewall and Gateway machines.

It is a relative simple task that can be accomplished with a tiny bit of dash, bash, perl, or your other favorite language, in a few if not just one line. However, having to figure it out 4 or more times a week adds up --especially now-days that I have better things to do than showing off my command line slutness to junior sysadmins. Here is the weed-out-from bash script I put together and officially added to my toolbox. It is certainly not the most efficient way of doing it but hey!

weed-out-from
#!/bin/bash
#weed-out-from - g0 2012 , aLog.IPduh.com

function help {
        echo "weed-out-from usage: weed-out-from infected-list list-of-weeds"
        exit 3
}

if [ -z $1 -o -z $2 ]
then
        help
fi
if [ ! -f $1 ]
then
        echo "weed-out-from: I cannot read the file $1"
        help
fi
if [ ! -f $2 ]
then
        echo "weed-out-from: I cannot read the file $2"
        help
fi

DIRTY=$1
DIRT=$2

for i in `cat $DIRTY`
do
        grep -i $i $DIRT &> /dev/null
        if [ $? -ne 0 ]
        then
                echo $i
        fi
done


weed-out-from usage:
$ cat spring-garden-before 
Windflower
groundsel
Azaleas
Allium
pigweed
Belladonna-Lily
Strelitzia
Bloodroot
Calla-Lily
Cornflower
Cosmos
kikuyugrass
Crocus
Crabgrass
Dahlia
spurge
Bermudagrass

$ cat list-of-weeds 
groundsel
pigweed
kikuyugrass
Crabgrass
spurge
Bermudagrass

$ weed-out-from spring-garden-before list-of-weeds 
Windflower
Azaleas
Allium
Belladonna-Lily
Strelitzia
Bloodroot
Calla-Lily
Cornflower
Cosmos
Crocus
Dahlia


Note: List elements may be separated by new lines or space.

Install weed-out-from for all users:
# wget http://kod.ipduh.com/lib/weed-out-from
# chmod 755 weed-out-from 
# mv weed-out-from /usr/bin


weed out a list from another list