help with grep and mailx

I have a huge file that I want to grep for the following:

the word "SEVERE" and today's month and date (Nov 16)

I then want to send the results of this grep to someone using mailx, as an atachment.

My question is how can I do this in one single line?

Thanks in advance.

check this out ....

cat testfile | grep "$(a=`date | cut -d" " -f 2,3`)" | grep "SEVERE" | tee file1 | uuencode file1 file1
| mailx X@Y.com

Can you explain the syntax? I tried this and it didn't work, so I'm trying to breakdown each command to be clear what it does.

Thanks

---> testfile is the 'HUGE' file u are mentioning abt ;

----> date | cut -d" " -f 2,3 gives Nov 16

---> grep "SEVERE" is grepping for the out put from prevoius pipe.

---> uuencode and mailx are used to send the file as attachement.

Please let me know what is err u are getting ;

Following is more efficient than previous :

grep -e "$(a=`date | cut -d" " -f 2,3`)|SEVERE" testfile | tee file1 | uuencode file1 file1 | mailx X@Y.com

sry for the previous one ...; it may not work ;

this will work :

grep "$(a=`date | cut -d" " -f 2,3`)" testfile | grep "SEVERE" | tee file1 | uuencode file1 file1 | mailx X@Y.com

Use $(command) or the old `command` but $(a=`command`) is crazy. What do you expect that to do?

I worked out the script Perderabo !!

Please let me know what's wrong with that ;

The theread starter wants the solution in single line that drove me to experiment like that ;

Please let me know if any thing wrong with that ;

I agree there may be simple solution for this .

I got yourr point .... ;

a=`date | cut -d" " -f 2,3`
grep "$a" testfile | grep "SEVERE" | tee file1 | uuencode file1 file1 | mailx X@Y.com

Might be good

You can feed date a parameter to make to output the date in various formats.

date | cut -d" " -f 2,3
date "+%b %d"

both produce the same result. I don't understand why you introduced that variable a. It seems to serve no purpose.

Try:
grep "$(date "%b %d")" testfile | grep "SEVERE" | tee file1 | uuencode file1 file1 | mailx X@Y.com

Thanks for all your replies. This has been a learning experience for me and I appreciate your help.

I have 2 minor changes...

The date it needs to look for has a space between the month and the day (Nov 17). Also, is there a way to have it not send the email if there are no severe errors? I know this probably can't be done in a single line, and that is OK.

Thanks again!

Help... I ran this line and I do get an email with an attachment, but the attached file is empty.

grep "$(date +"%b %d")" order.log|grep "SEVERE"|tee ord_err.log|uuencode ord_err.log ord_err.log|mailx -s "test"
xyz@xyz.com

I ran just this part of it: grep "$(date +"%b %d")" order.log|grep "SEVERE" and I do get output as seen below:

Nov 18 05:02:26 [SEVERE] Ord <R00055430405-SFI> has status <S> and therefore can't be deleted.
Nov 18 08:47:17 [SEVERE] Ord <P00000553218-SFI> has status <S> and therefore can't be deleted.
Nov 18 08:47:17 [SEVERE] Ord <P00000554462-SFI> has status <S> and therefore can't be deleted.
Nov 18 08:47:17 [SEVERE] Ord <P00000553781-SFI> has status <S> and therefore can't be deleted.
Nov 18 08:47:17 [SEVERE] Ord <P00000552862-SFI> has status <S> and therefore can't be deleted.
Nov 18 08:47:17 [SEVERE] Ord <P00000553084-SFI> has status <S> and therefore can't be deleted.
Nov 18 08:47:17 [SEVERE] Ord <P00000553640-SFI> has status <S> and therefore can't be deleted.
Nov 18 08:47:17 [SEVERE] Ord <P00000550070-SFI> has status <S> and therefore can't be deleted.
Nov 18 08:47:17 [SEVERE] Ord <P00000553780-SFI> has status <S> and therefore can't be deleted.

Question is, why is it sending a blank file?

I do n't see any prblem with that ;
It is perfectly working for me ;

Do one thing ... ;

Check file1 which is created in yr directory. Check/See its size.

Also

Test uuencode with some dummy text file to check whether second part is working or not ;

Or try ... sendmail ....

The file size of order.log is 0, even though there are records in it that file that are both SEVERE and have a date of Nov 18.

After I get this to work, is there a way to code it so that it does the grep and if there are no errors, simply exits?

Steve

There is a problem with syntax like:

tee file1 | uuencode file1 file1 | mailx X@Y.com

When uuencode has two parameters, the first is the input file and that means that uuencode will not be reading from stdin thus breaking the pipeline. At the time uuencode reads from file1, the tee program may or may not have written to it. It depends on the order in which the programs in the pipeline are launched, the number of cpu's, etc.

If you really need file1 to exist on the local system try:

tee file1 | uuencode file1 | mailx X@Y.com

if not just go with:

uuencode file1 | mailx X@Y.com

The syntax to handle the zerio length file will depend on what shell you are using.

I'm using KSH

Then try:
grep "$(date +"%b %d")" order.log|grep "SEVERE" > file1 ; [[ -s file1 ]] && uuencode file1 file1 | mailx X@Y.com

Would it be possible to store a list of email addresses in another file and have this code loop through this list, sending the email to every recipient in this list?

You can have more than one recipient with mailx. Just tack them on the end. Or create an alias and send the mail to the alias. Either is more efficient than looping.