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.
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:
#!/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
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.
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 ~