Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Removing Lines

Status
Not open for further replies.

skotapal

Programmer
Joined
Aug 26, 2002
Messages
2
Location
US
Hi
I am trying to write a script that will
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 removed. It is overwriting the file ipaccess after grepping for each on the known IPs. Can any one help??

Regards

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 $IP_ADD $SOURCE_DIR/ips > $SOURCE_DIR/ipaccess
done
uuencode $SOURCE_DIR/ips | mail -s "IPs Accessing mywebserver.com today" ksrinivas

 
Does

grep -v $IP_ADD $SOURCE_DIR/ips > $SOURCE_DIR/ipaccess

do what you want? CaKiwi
 
Srini:

You are only getting the last one because your redirection always overwrites $SOURCE_DIR/ipaccess, thus you only get the last one. You need to append:

grep $IP_ADD $SOURCE_DIR/ips >> $SOURCE_DIR/ipaccess


Regards,

Ed
 
I had already grepped with the -v option (it works partially. redirecting with a >> gives me the output of all the greps. I need a consolidated file which has the IPs listed in the list file removed in the final file.

Thanks for the quick replies guys!!

Regards

Srini
 
Replace your whole 'cat | while' loop with:
Code:
grep -v -f $IP_LIST $SOURCE_DIR/ips > $SOURCE_DIR/ipaccess

From man grep:
Code:
-f	pattern_file
Read one or more patterns from the file named by the pathname pattern_file.  Patterns in pattern_file are terminated by a newline character. A null pattern can be specified by an empty line in pattern_file.  Unless the -E or -F option is also specified, each pattern will be treated as a BRE, as described in the regcomp(5) man page under the section titled: Basic Regular Expressions.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top