Loop and grepping into a file

I wrote this script for:

  1. Get the Web log for today
  2. Give me a list of all the IP addresses that have accessed the web server today
  3. Remove a list of known IPs listed in a file (line by line)
  4. Mail the final file to selected recipients.
    I am unable to do part 3. In the script pasted below, I can get only the last IP address from the list removed. It is overwriting the file ipaccess after grepping for each on the known IPs. When I use the >> option it appends to the file. But I want a consolidated list.

Can u gimme some idea how to finish it up?

Srini

#! /bin/sh

IP_LIST=/home/ksrinivas/knownips
SOURCE_DIR=/disk2/MUSTANG/
currentDate=`date +%d"/"%b"/"%Y`

awk ' {print $1" "$4" "$7}' /etc/httpd/logs/access_log | sed /exe/D | grep "$currentDate" > $SOURCE_DIR/access.txt

awk ' {print $1}' $SOURCE_DIR/access.txt > $SOURCE_DIR/ip-add

sort -u $SOURCE_DIR/ip-add > $SOURCE_DIR/ips

cat $IP_LIST | while read IP_ADD
do
grep -v $IP_ADD $SOURCE_DIR/ips >> $SOURCE_DIR/ipaccess
done

uuencode $SOURCE_DIR/ipaccess | mail -s "IPs Accessing mywebserver.com today" ksrinivas

Instead of using a loop and grep -v each line, try this:

[...]

grep -vf $IP_LIST $SOURCE_DIR/ips > $SOURCE_DIR/ipaccess 

[...]

That does away with the loop altogether...
See man grep for more info on the -f flag.

try this...

cat $SOURCE_DIR/ips | while read IP_ADD
do
grep -q $IP_ADD $IP_LIST
if [ $? -ne 0 ]
then
echo $IP_ADD >> thirdfile
fi
done

the file thirdfile should have the all those ips in file "ips" which are not present in file "$IP_LIST"...

I currently don't have UNIX access to test this out... but I think you got the point! :slight_smile:

alternately you could have also used this kind of loop... though it is not anyway better... this doesn't use piping...

for IP_ADD in `cat $SOURCE_DIR/ips`
do
....
done

surely the solution given by LivinFree is the most elegant!!!

it pays of reading the manual for even most "common" commands for their treasury of options....