Help with grep and read function in a BASH Script

I'm putting together a script that will search my mail archives for emails that meet certain criteria and output the files to a text file.

I can manually cat that text file and pipe it into sendmail and it will work (i.e. cat /pathtofile/foo.txt | sendmail -t me@company.com)

My script sends a list of the files to a text file and is suppost to read the filename on each line and email it to me but all im getting is a text blank emails so it doesn't appear to be reading lines correctly.
(i.e. cat /tmp/foo.txt | while read i; echo "$i" | sendmail -t me@company.com)

I don't know if it makes a diffrence but the text file lines are automatically formatted like this:

./1312288621.V804I59c1aaa3M731667.sam.company.local:To: "My Name" <me@company.com>,
./1312289223.V804I59c1aad4M503269.sam.company.local:To: "My Name" <me@company.com>

The only parts that of the file that are needed are:

1312288621.V804I59c1aaa3M731667.sam.company.local
1312289223.V804I59c1aad4M503269.sam.company.local

Use grep -l ?

Maybe I explained it wrong,

I'm trying to strip the ./ from the begining of every line
and then strip everything after sam.company.local

awk -F"[/:]" '{print $2}' foo.txt | while read line; do echo "$line"; done;

Do you know if a way to cat or pipe the contents of the awk line that you gave me?

I try to pipe it and it gives me an error and if I redirect it it gives me a empty file

awk -F"[/:]" '{print $2}' foo.txt >> outfile
[root@localhost ugr]# cat outfile
1312288621.V804I59c1aaa3M731667.sam.company.local
1312289223.V804I59c1aad4M503269.sam.company.local

this works fine with me, im testing it in Red Hat 5.5

1 Like

odd, I wonder if ubuntu interpretes awk diffrently

awk -F"[/:]" '{print $2}' /tmp/foo.txt | while read line; do echo "$line"; done;

prints to screen

but the following just gives me a empty file

awk -F"[/:]" '{print $2}' /tmp/foo.txt | while read line; do echo "$line"; done; >> /tmp/foo2.txt

just take this part, because i used while do loop to show its right usage

awk -F"[/:]" '{print $2}' /tmp/foo.txt >> /tmp/foo2.txt

besides as you used ";" after "done" it gives nothing, so i think you need this:

awk -F"[/:]" '{print $2}' /tmp/foo.txt | while read line; do echo "$line" && sendmail -t me@mycompany.com; done