2.6.2010

compare 2 lists in bash

Filed under: bash — nax @ 15:00

Interesting task – compare 2 lists (in my case hostnames). You can use diff, but need to sort | unique them first. Rather I wrote a very simple script to do it:

#!/bin/bash
FILE1=$1
FILE2=$2

cat $FILE1 | \
while read ITEM ; 
do 
	grep -i "^$ITEM$" $FILE2 &> /dev/null
	echo $ITEM,$?
done | \
grep '1$' | \
cut -f1 -d,

And second very simple script to convert for SQL WHERE HOST_NAME IN () notation:

#!/bin/bash

while read ITEM
do
	echo -n \'$ITEM\',
done

Nice exercises for bash while construct, isn’t it?

Comments are closed.

Powered by WordPress