Using Grep to Find All Emails

grep regex to find emails of a certain tld

Do:

grep '@.\+\.com$' file.txt
  • @.\+ matches a @ followed by one or more characters
  • \.com$ matches literal .com at the end

to do the same for other TLDs, replace com at the end with that TLD.

grep regex match email address

The problem is that grep matches anything in a line. If you want the exact whole line, add the $ terminator at the end. Let's look at an example:

ABCabc+-._@school3-IT.image.tor.chrome.canadannn
  1. ABCabc+-._ matches ^[A-Za-z0-9+._-]+
  2. @ matches @
  3. school3-IT.image.tor.chrome. matches ([a-zA-Z0-9-]+\.)+. As far as I know, all quantifiers are greedy in grep.
  4. canada matches [a-zA-Z]{2,6}
  5. nnn gets ignored

Without the $, there just has to be some part of the line that matches, not necessarily the whole thing.

Extract email addresses from log with grep or sed

With GNU grep, just use a regular expression containing a look behind and look ahead:

$ grep -Po '(?<=to=<).*(?=>)' file
wanted1918_ke@yahoo.com
devi_joshi@yahoo.com

This says: hey, extract all the strings preceded by to=< and followed by >.

How to extract emails from logs using grep

You can use awk to both check if the "Successfully done something." line occurs and also it contains an email:

awk '/Successfully done something. Email:/ && \  # match line
match($0, /Email: ([^ ]*) /, matches) { # match up to space
print matches[1] # print captured group
}' file

With your given data:

$ awk '/Successfully done something. Email:/ && match($0, /Email: ([^ ]*) /, matches) {print matches[1]}' file
foo@bar.com


Related Topics



Leave a reply



Submit