I was given the task to see if we are advertising a list of ip addresses(3000). Not a good idea to do it manually, so I copied all the ip addresses that we are advertising i
A better script :
while read ip
do
grep "$ip" "$ips" > /dev/null 2>&1 && echo "$ip" >> ip.found || echo "$ip" >> ip.notfound
done
Name the script "searchip.sh"
Assume your input file is "iplist" ,set up variable and call like this:
ips=ips
cat iplist | sh searchip.sh
or
sh searchip.sh < iplist
Then you get two files , one is ip found, other one is ip not found.
What you need is shell I/O redirection.
If you sort the two files, you can use the comm command:
sort all_ip_addresses > all_ip_addresses_sorted
sort adverted_ip_address > advertised_ip_address_unsorted
comm -23 all_ip_addresses_sorted advertised_ip_addresses_sorted
will show the IP addresses that are not advertised, and:
comm -12 all_ip_addresses_sorted advertised_ip_addresses_sorted
will show the advertised IP addresses.
You can also avoid creating the separate sorted files by using process substitution:
comm -23 <(sort all_ip_addresses) <(sort advertised_ip_addresses)
$ script < list_of_ip_addresses
That's all you need.