I am writing a bash script which accepts a list of CSV files as arguments and outputs e-mail addresses only found in the first file. To accomplish this, for each record in the first CSV file I look up the e-mail address field and read its contents into a shell variable. Then, I use grep -iE
with the following regular expression to look up the e-mail address just found in all the remaining files, making sure that it is not a substring (e.g. [email protected] is not the same as [email protected]), and allowing it to be at the beginning or end of a record:
"^(.*,)?($EMAIL_ADDRESS|\"$EMAIL_ADDRESS\")(,.*)?\$"
A problem with this approach is that e-mail addresses contain dots which have a special meaning in regular expressions. My questions are:
- How can I avoid this problem in an elegant way?
- How can I avoid this problem in a more general context, e.g. when the value to look up is not an e-mail address but some free text and might contain other special characters as well?
awk -F, '$2 == "[email protected]"'
?\.
in front of the dot to escape it. you probably need two backslashes\\.
to get the shell to pass one to the program.grep -F
to do not treat pattern as regular expression, just like a string and-w
option (whole word) which mean that pattern should fill full "word" ( so "he@
" is not compare " she@
")