Script to consolidate files as mails,groupid

Hi, I'm newbie in Bash scripting.
Here is scenario I've a file which containslist of 100 mailgroup and in a directory i have 100 files with each group's respective mail addresses.

I'd like to make one CSV file with this information as follows.

consolidated files with

mailid,groupname.

Please help me to make script for this.

cheers.

too less informations. if you need help, provide some samples input files first.

I've got one group.txt file. In this file there are 120 mailgroups like

hostaff
financedept
accountsdept
itdept
...
...
etc etc

I've extracted mail ids in these groups file names as hostaff.txt contains
abc@example.com
bsd@example.com
sde@example.com
...
...
etc

I', like to fill all group names besides mail ids in one file as follows

abc@example.com,hostaff
bsd@example.com,hostaff
trer@example.com,financedept
sds@example.com,finacedept
srw@example.com,accountsdept
...
...

Please Help me to make a script..

Cheers.

not clear.

Do you mean assign each mailgroup assign to two mailboxes only?

No, if there is a mailids files with 100 mailids than in consolidated file it has to show mailid1,mailgroup
mailid2,mailgroup
mailid3,mailgroup like that

I've tried it with sed like follows

in shell it works perfect like

sed 's/$/,mailgroup/g' mailidsfile > consolidated.txt

but in bash script it does not work. my script is as follows

#!/bin/sh
file=/home/test/mailgroups.txt
for i in file
do sed 's/$/,$file/g' $file > consolidated.txt
done

it gives error : sed: -e expression #1, char 11: unterminated `s' command

Cheers

Ok, for each mailbox, you need attach all mailgroups on it.

Try this:

awk 'NR==FNR{a[$1];next}{for (i in a) print $0 "," i}' group.txt mail_ids.txt

abc@example.com,accountsdept
abc@example.com,hostaff
abc@example.com,financedept
abc@example.com,itdept
bsd@example.com,accountsdept
bsd@example.com,hostaff
bsd@example.com,financedept
bsd@example.com,itdept
sde@example.com ,accountsdept
sde@example.com ,hostaff
sde@example.com ,financedept
sde@example.com ,itdept

Thanks for your help
It has not completed my requirement.

Let me explain in detail.

I've a folder which has 100 mail ids files under groupnames.

Ex:

accountdept.txt file contains 100 mailbox list

abc@example.com
bcd@example.com
sds@example.com

hostaff.txt contains 500 mailbox list
financedept.txt contains 30 mailbox list

There are 100 group files in a folder each contains 50-500 mail ids

I want to export all the information in a single file with either mailid,mailgroup format
OR each mailgroup header with mailids under that list

Please help me how to get that.

Cheers

Maybe this time I get it.

So you just need export the mailbox with its filename (because the filename includes the group name already).

awk '{split(FILENAME,a,".");print $0 "," a[1]}' *.txt