Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

find and replace (long e-mails vs aliases) 1

Status
Not open for further replies.

tekiila

Technical User
Jun 28, 2006
4
EE
Hey!

I'm new to scripting so I hope You can help.

I have /etc/aliases file where are e-mails and aliases, for example:

john.smith@asd.com john
mary.smith@asd.com msmith

And I have e-mail list with people in that list as a file. The problem is that aliases and e-mails are mixed. Like:

john.smith@asd.com
msmith

But I'd like to have there ONLY e-mails. Like:

john.smith@asd.com
mary.smith@asd.com

So I was trying to use awk to sort it out, but it seems that it's bit complicated...

Here's what I got:

#!/bin/bash

for short_email_list in `cat short_ones`
do
short_name=`echo $short_email_list | awk -F \@ '{ print $1}'`
full_name=`grep :"[[:space:]]\{1,\}"$short_name /etc/aliases | grep "\." | awk -F : ' {print $1} '`
echo "Full name:" $full_name ", Short name:" $short_name
done

Could anyone help out with that?

Thank You!
 
Why not just grep the file for "@"?

Although the script you posted doesn't seem to match the requirements you describe...

Can you give an example chunk of the input file and an example of the output you would like from that?

Annihilannic.
 
Hey!

Cant just grep because there are different people in different lists. etc/aliases contains everyone, those too who arent in any mailinglists.

Like I said before: I have 2 files:

1. /etc/aliases/ which contains adresses and aliases like that:

john.smith@asd.com john
mary.smith@asd.com msmith

2. and different mailinglists which contains e-mails AND aliases mixed.

john.smith@asd.com
msmith

But I need to have there ONLY e-mails.

john.smith@asd.com
mary.smith@asd.com

tekiila
 
Hi

Is still not clear how would be 1 result from 2 files. One possibility :
Code:
grep -h '@' /etc/aliases /other/file | cut -d ' ' -f 1 | sort -u
But while this is an [tt]awk[/tt] forum :
Code:
awk '/@/{if(!a[$1]){print$1;a[$1]=1}}' /etc/aliases /other/file

Feherke.
 
I think I understand the requirement now... basically you want to update the mailing list with the correct email address from /etc/aliases if it is using the short alias name.

How about this:

Code:
awk '
        # populate the alias array
        FILENAME==ARGV[1] { aliases[$2]=$1 }
        # it is a normal address, print it
        FILENAME==ARGV[2] && /@/ { print }
        # it is an alias, look up the full address
        FILENAME==ARGV[2] && !/@/ { print aliases[$1] }
' /etc/aliases mailinglist

Annihilannic.
 
:)

Mailinglist is mixed: e-mails and aliases. So I wanted the script to look up what e-mail is for alias in mailinglist from /etc/aliases and replace the alias with e-mail.

Thanks,
tekiila
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top