The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Grepping the last 30 minutes of a log file... jtelep Shell Programming and Scripting 2 03-06-2008 12:27 AM
grepping for period . in file name ? ie grep . bobk544 UNIX for Dummies Questions & Answers 5 12-01-2007 02:31 AM
Grepping Errors in a file achararun Shell Programming and Scripting 4 02-05-2007 03:36 AM
grepping the first 3 characters from a file rachael UNIX for Dummies Questions & Answers 2 10-15-2001 02:33 PM
grepping the first 3 characters from a file g-e-n-o UNIX for Dummies Questions & Answers 2 10-15-2001 06:11 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 08-26-2002
skotapal skotapal is offline
Registered User
  
 

Join Date: Aug 2002
Location: Philadelphia, PA
Posts: 75
Question 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
  #2 (permalink)  
Old 08-26-2002
LivinFree's Avatar
LivinFree LivinFree is offline Forum Advisor  
Goober Extraordinaire
  
 

Join Date: Jul 2001
Location: Portland, OR, USA
Posts: 1,584
Instead of using a loop and grep -v each line, try this:
Code:
[...]

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.

Last edited by LivinFree; 08-26-2002 at 10:36 PM..
  #3 (permalink)  
Old 08-26-2002
Vishnu Vishnu is offline
Registered User
  
 

Join Date: Aug 2002
Location: Marlboro, MA
Posts: 114
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!

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
  #4 (permalink)  
Old 08-26-2002
Vishnu Vishnu is offline
Registered User
  
 

Join Date: Aug 2002
Location: Marlboro, MA
Posts: 114
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....
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 01:00 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0