compare 2 lists in bash

Written in

by

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?

Tags

Napsat komentář