need help in finding a string and to send an email using shell script

Hi All

i am writing a shell script which will search for a string "expires". once the search string is found it has to give the email address as the output and send an email to the person

This is basically to find the encrypetd keys which are loaded in the unix server

Below are sample keys not the true values:

pub 5858D/58585255EF 2007-10-02 unicorn01516008 <uniccrn@fund.com>
sub 452659g/97974545df 2007-10-02 [expires: 2008-06-22]

once i run the shell script it will execute the command gpg --list-keys and then search for the string and the output should be like this ::

unicorn01516008 <uniccrn@fund.com> Expires on 2008-06-22 and this has to be emailed to some xxxx@xxxx.com

can you please help me in giving me some sample code !!!

Thanks a lot for your help!!!

Is the text in the file

pub 5858D/58585255EF 2007-10-02 unicorn01516008 <uniccrn@fund.com>
sub 452659g/97974545df 2007-10-02 [expires: 2008-06-22]

all on one line? Thus, one line per license key entry?

and are these entries all within some file called xxx.txt?

They are in seperate lines

all the informations are loaded into xxx.txt

Thanks!!

pub 5858D/58585255EF 2007-10-02 unicorn01516008 <uniccrn@fund.com>
sub 452659g/97974545df 2007-10-02 [expires: 2008-06-22]

They are in seperate lines but if it searches for expires , it has to get this result (unicorn01516008 <uniccrn@fund.com>) as they are interrelated

and all these informations are loaded in some output files xxx.txt

#! /bin/bash

admin=me@mycompany.com

rm sample_key.mat 2>/dev/null
cat sample_key.txt | grep "expire" >sample_key.mat

while read zf
   do
#      echo $zf
      fld1=$(echo $zf | cut -d" " -f4)
      fld2=$(echo $zf | cut -d" " -f5)
      fld3=$(echo $zf | cut -d" " -f9)
      fld4=$(echo $zf | cut -d" " -f10)

      subj="Expiring certificate"  
      mesg="$fld1 $fld2 $fld3 $fld4"
      echo $mesg

      echo "$mesg" | mailx -s "$subj" "$admin" 


done < sample_key.mat

rm sample_key.mat 2>/dev/null

Make sure to adjust the admin variable at the top. Seemed to work with a file which I used as a sample:

cat sample_key.txt
pub 5858D/58585255EF 2007-10-02 unicorn01516008 <uniccrn1@fund.com> sub 452659g/97974545df 2007-10-02 [expires: 2008-06-22]
pub 5858D/58585255EF 2007-10-02 unicorn01516008 <uniccrn2@fund.com> sub 452659g/97974545df 2007-10-02 [expires: 2008-07-22]
pub 5858D/58585255EF 2007-10-02 unicorn01516008 <uniccrn3@fund.com> sub 452659g/97974545df 2007-10-02 [expires: 2008-08-22]
pub 5858D/58585255EF 2007-10-02 unicorn01516008 <uniccrn4@fund.com> sub 452659g/97974545df 2007-10-02 
pub 5858D/58585255EF 2007-10-02 unicorn01516008 <uniccrn5@fund.com> sub 452659g/97974545df 2007-10-02 [expires: 2008-09-22]
pub 5858D/58585255EF 2007-10-02 unicorn01516008 <uniccrn6@fund.com> sub 452659g/97974545df 2007-10-02
pub 5858D/58585255EF 2007-10-02 unicorn01516008 <uniccrn7@fund.com> sub 452659g/97974545df 2007-10-02 [expires: 2008-07-22]

If my output file is having double line will it work , also if the length varies in my keys , this will work ??

Thanks Again!!

Hi i tried with a sample file which has the list of keys

it gave me this output [expires: 2008-05-16]

but the require output is unicorn01516008 <uniccrn1@fund.com> [expires: 2008-05-16]

can you please correct me if i am doing anything wrong!!

What I wrote was for a one-line log file. It was unclear from what you originally submitted to this board if the data was all on one line. It appears you posted the answer - data on two lines - while I was working on a solution and posting.

So, while the script written does not care how many characters in each field, it does assume that all ten fields are across one line. Thus, if the data is in two lines, you would get the results you just wrote.

I will have to think about how to modify the script to pull the two lines together. Unless, anyone else on the Board has ideas...

a few questions...
-always two and only two lines?
-any data in common between the two lines, like a code value?
-are the lines always in the pub ... sub ... order?

Thanks for the reply

Not always there will be two lines ..

There will be pub and sub and also in some lines there will be a uid as well

also .. with that ouput i have to give it like this

Name&email Expires
=========== ===========
<xxxx.@xxx.com> [2007-10-08]

Could try to figure out a solution, but really need to see more than the small piece of data - ideally ten or more lines. Also, the desired output.

I think i am all set with the above script , its working fine

Just an addon to it can we bring the output like this ::

Name&email Expires
=========== ===========
<xxxx.@xxx.com> [2007-10-08]