Friday, September 4, 2015

Generate email filters in Gmail from list of emails

I have a lot of students from different classes who email me regularly. It is helpful to tell who belongs to which class. So I created a filter in Gmail which will sort the student's email according to their email ID. This scenario is also applicable to industry where we want to sort emails using filters. Most of the time people are able to do it by hand. However, for me it was challenging to do it by hand since I had 400 students. Therefore I fell back to Bash tools to automate the task.

The first thing is to extract all the email IDs from a CSV file and save it as a list.

cat file.csv | cut -d"," -f10

The 10th column had the email IDs in the CSV file. The fields are separated by commas in CSV files. Therefore, I have used -d"," as the delimiter in the cut command.

"rwrwfa"
"ebbra"
"rksda"
"ru57a"

But they have the " which I need to get rid off. Easy use tr.

cat file.csv | cut -d"," -f12 | sed 's/"//'

rwrwfa"
ebbra"
rksda"
ru57a"

The reason why I didn't get rid off the last " is because I am going to replace that with @aaa.edu since these email IDs do no have the @aaa.edu in them.

cat file.csv | cut -d"," -f12 | sed 's/"//' | sed 's/"/@aau.edu'

"rwrwfa@aaa.edu"
"ebbra@aaa.edu"
"rksda@aaa.edu"
"ru57a@aaa.edu"

Save this list to a file tempfile1.

cat file.csv | cut -d"," -f12 | sed 's/"//' | sed 's/"/@aau.edu' > tempfile1

The reason why I can use . (dot) inside sed is because I used  ' ' instead of " "  so .(dot) is interpreted as . (dot) and not as a wildcard. 








2 comments:

SS said...

Cool tutorial.

Sudipta Ray said...

The title is kind of a tongue-twister in itself..:)

OK GOOGLE on Samsung Galaxy S7 doesn’t work

To make Ok Google detection work on Galaxy S7 (Galaxy series phones) we need to perform a couple of steps. 1. As long as Samsung S vo...