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
Grep commands in loop karthikn7974 Shell Programming and Scripting 0 04-28-2008 09:16 AM
while loop inside while loop panknil Shell Programming and Scripting 0 01-07-2008 12:49 PM
grep and awk showing filename in loop sjohns6 Shell Programming and Scripting 5 05-24-2007 08:59 AM
grep -v while loop bobo UNIX for Dummies Questions & Answers 8 01-26-2007 10:53 PM
how to get the similar function in while loop or for loop trynew Shell Programming and Scripting 3 06-17-2002 11:09 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 03-28-2005
gundu gundu is offline
Registered User
  
 

Join Date: Mar 2005
Posts: 42
grep in a loop

Hi ,

I am trying a script which takes user input userid . I am stuck how to check whether that is a valid user id or not in the audit log files. My code is :

cd $CCP_AUDIT
cat * > /export/home/$USR/l***/files
echo "UserId:\c"
read UserId

#Date Function
echo "DATE [YYYY-MM-DD] : \c"
read xxx

I need help in putting this in a loop:

grep $xxx /export/home/$USR/lalitha/files |grep -v Created |grep -v Rejecte
d | grep -i $UserId | Mail -s "Transactions for $UserId" ***@***.com
~
Thanks,
Gundu
  #2 (permalink)  
Old 03-28-2005
criglerj's Avatar
criglerj criglerj is offline
Registered User
  
 

Join Date: May 2002
Location: Atlanta
Posts: 129
With all the following, YM,aa,MV.

If I understand your problem correctly, you are trying to find all the log entries on a given date for a given user, then send an email to someone (probably yourself) to match all the transactions except Create and Reject.

You're asking specifically about putting the "grep" pipeline in a loop. This is not an especially big problem. Do you want to loop based on entering different user names? Different dates? Both? This simplest method is something like this (using /bin/sh):

Code:
while true; do
    read UserId
    read xxx
    grep [as you originally posted it]
done
(You probably want to put your prompts in as well --- this is just to show the general method.)

Then when you've run the program and for all the dates and userids, just hit control-C to break out of the loop.

More comments follow in another post.
  #3 (permalink)  
Old 03-28-2005
gundu gundu is offline
Registered User
  
 

Join Date: Mar 2005
Posts: 42
Thanks,

I like the way it is giving me option , but if the userid is not there then i need to break out saying userid doesnot exist, how to do that??

Thanks,
Gundu
  #4 (permalink)  
Old 03-28-2005
criglerj's Avatar
criglerj criglerj is offline
Registered User
  
 

Join Date: May 2002
Location: Atlanta
Posts: 129
Quote:
Originally Posted by gundu
Thanks,

I like the way it is giving me option , but if the userid is not there then i need to break out saying userid doesnot exist, how to do that??

Thanks,
Gundu
Do you mean you want to break out of the loop if there is no log entry for a given user?
  #5 (permalink)  
Old 03-28-2005
gundu gundu is offline
Registered User
  
 

Join Date: Mar 2005
Posts: 42
yes, i take the user input and check to see it is there in the log file for that date and userid, if not exit with echo doesn't exist

Thanks,
Gundu
  #6 (permalink)  
Old 03-28-2005
criglerj's Avatar
criglerj criglerj is offline
Registered User
  
 

Join Date: May 2002
Location: Atlanta
Posts: 129
Quote:
Originally Posted by gundu
yes, i take the user input and check to see it is there in the log file for that date and userid, if not exit with echo doesn't exist
I'm taking this to mean there are zero lines in the resulting output. (This is a different question than whether a given userid exists.)

Okay, this is where a temporary file begins to make sense: You need the same intermediate data for two different purposes. So change it this way:

Code:
grep ... > tempfile
if [ -s tempfile ]; then
    Mail ... < tempfile
else
    echo No such userid $UserId >&2
    break
fi
rm tempfile
  #7 (permalink)  
Old 03-28-2005
criglerj's Avatar
criglerj criglerj is offline
Registered User
  
 

Join Date: May 2002
Location: Atlanta
Posts: 129
cat-ting to a temporary file

Your 'cat * > tempfile' is a classic UUOC (useless use of cat). There's nothing wrong with making '*' (without the apostrophes so it gets expanded) the "argument" to the first command in your pipeline.

Next, when you have four greps in a pipeline, you should consider using something like awk:

Code:
awk "/$xxx/ && /$UserId/ && !/Created/ && !/Rejected/" * | Mail ...
Notice the "double quotes" around the awk script are not 'apostrophes' --- you need your variables to be expanded. Of course, If you need a "$" in your search pattern, you're in for quoting experiments ...

(Added) I forgot to mention that this search is not case-insensitive, unlike your original grep.

Last edited by criglerj; 03-28-2005 at 04:48 PM..
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 10:21 AM.


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