![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
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 |
|
|||||
|
Quote:
|
|
|||||
|
Quote:
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
|
|
|||||
|
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 ... (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.. |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|