For Loop and concetnating values in a variable

Hi,

I have file abc.txt which has keys and emails addresses

abc.txt
emailkey1:sam@abc.com
emailkey1:tom@abc.com
emailkey2:rqw@abc.com
emailkey2:tut@abc.com

I have a shell script where i pass key as the parameter and i want all the email addresses within that key concatenated by a comma

E.g if i pass emailkey1.... it should return sam@abc.com,tom@abc.com

Please help

Thanks much in advance

Sam

can you share you script you have so far?

Its a simple script.. wherein i just pass emailkey as the parameter. It shoud search for all the email addresses corresponding to the email key , based on which i have to send emails to people.

It may be necessarily through a for loop.

so where's the script that you have?

Let me put this in better words

I want to write a shell script where i pass key as the parameter and i want all the email addresses within that key concatenated by a comma

one way:

nawk -F: -v OFS=',' -v key='emailkey1' '$1==key {arr[key] = (key in arr) ? arr[key] OFS $2 : $2} END {print arr[key]}' abc.txt

Vow!!

Any other simpler way for a novoice like me to understand :slight_smile:

My method is similar (not as slick), but doesn't use arrays...

awk -F: -v key="emailkey2" '
 BEGIN { addrlist=0 }
 $1 ~ key {
   addr=$2
   if (addrlist == 0) {
     addrlist=addr
   } else {
     addrlist=addrlist "," addr
   }
 }
 END { print addrlist }' abc.txt

or a little bit verbose with the ksh

./samit.sh emailkey1

samit.sh:

#!/bin/ksh

key="${1}"

file='abc.txt'

while IFS=':' read keyCurrent addr
do
   if [ "${keyCurrent}" = "${key}" ]; then
      [ "${first}" -eq 0 ] && printf "${addr}" ||  printf ",${addr}"
      first=$(($first + 1))
   fi

done < "${file}"
printf "\n"

Thanks for those answers. Appreciate your help

i tried something from my end

for sAddress in `grep ^emailkey1 abc.txt | sed 's/^.*\://'`
do
sMailGroup=$sAddress","$sMailGroup
done

Thanks once again!!!

Sam

If speed is a consideration then you will want to stick with an AWK solution.

If the keys are identical for more than one user, why not put both, or all emails with the same key on one line using the comma delimter you need anyway. Then you could just use something simple like:

sMailGroup='grep ^emailkey1 abc.txt | awk -F":" '{print $NF}'`

Simple approach...

$ grep emailkey1 abc.txt
emailkey1:sam@abc.com
emailkey1:tom@abc.com

$ grep emailkey1 abc.txt | cut -d: -f2
sam@abc.com
tom@abc.com

$ grep emailkey1 abc.txt | cut -d: -f2 | paste -s -d,
sam@abc.com,tom@abc.com

Alternative in Python

#!/usr/bin/python
input = raw_input("Enter email key:" )
all = open("abc.txt").readlines()
email = [items.strip().split(":")[-1] for items in all if input in items]
print "%s:" % input , ','.join(email)

Output:

/home:> python test.py
Enter email key:emailkey1
emailkey1: sam@abc.com,tom@abc.com

Thanks all of you for the various solutions.

Appreciate your help

Hi Ygor,

I tried using this

grep emailkey1 abc.txt | cut -d: -f2 | paste -s -d,

However it errored out saying

paste: Usage: paste [-s] [-d List ] File1 ...

It worked for me. Perhaps your version of paste needs space between "-d" and ","?

Dear vgersh99,

I have one question about your code, normally we refer to an array by the subscript where subscript is a number, but in your code you have used arr[key] where key='emailkey1' how it works? and how awk keeps track of subscript in that case? And while printing also you have used the same thing that print arr[key] and it prints all the elements of array based on subscript arr[emailkey1], its really confusing for me.

Your explanation would be highly appreciated.

Cheers,
An Awk student

Dear Glen,

Just want to clarify one thing about $1 ~ key, that here match operator ~ was not required, because as per my understanding, match operator is required only in case where we have to compare a value against a regular expression, for comparing values against variables or constants we can use something like $1==key am I right?

It would be hightly appreciated if you can elaborate the detailed use of match operator ~

Cheers,
An Awk student

awk's arrays are associaitive in nature where the subscript is a 'string' and not necessarilly a 'number'.

Iterating through an array based on its subscriipt [a 'key'] we print the content of every array cell [its 'address'].