1

I'm using Linux Centos 6-64.

How can I change the following command, which only retrieves emails written with @, inside an input folder:

grep -E -o -r "[A-Za-z0-9][A-Za-z0-9._%+-]+@[A-Za-z0-9][A-Za-z0-9.-]+\.[A-Za-z]{2,6}" /inputfolder/ | sort | uniq > "/outputfolder/result.txt"

to retrieve emails in all these @ formats below?

info@example.com
info@example.com
info(at)example.com
info[at]example.com
info%26%23064%3Bexample.com
info%40example.com

If possible command in one single line. Thank you

  • 1
    You can use groups like (foo|bar) instead of the literal @. – slhck Aug 3 '18 at 8:17
  • Decent start with formatting. Downvote revoked. – Kamil Maciorowski Aug 3 '18 at 8:55
  • Hello Kamil, have formatted it, please let me know if you find it more pleasant now. By the way would you know what need to update in this command? Thanks – Joao Aug 3 '18 at 8:55
  • Hint: to address another user in a comment, the syntax is like @KamilMaciorowski . Please read "Replying in comments" section here. The point is "Hello Kamil" won't send me any notification but "@Kamil" will. – Kamil Maciorowski Aug 3 '18 at 8:59
  • 1
    Joao, obviously, you should not use (foo|bar), as that will only match if someone literally typed foo or bar. That was just an example. You need to replace foo and bar with the respective alternatives for the @ that you want to capture. – slhck Aug 3 '18 at 9:11
1

You could use -P flag for perl regex and -i for case insensitive:

grep -Pi "[A-Z0-9][\w.%+-]+(?:@|@|\(at\)|\[at\]|%26%23064%3B|%40)[A-Z0-9][A-Z0-9.-]+\.[A-Z]{2,6}"

\w stands for [a-zA-Z0-9_]

Explanation:

[A-Z0-9]            : 1 alphanum
[\w.%+-]+           : 1 or more alphanum, _, ., %, + or -
(?:                 : start non capture group
    @               : literally @
  |                 : OR
    @          : html entity for @
  |                 :
    \(at\)          :
  |                 :
    \[at\]          :
  |                 :
    %26%23064%3B    :
  |                 :
    %40             :
)                   :
[A-Z0-9]            : 1 alphanum
[A-Z0-9.-]+         : 1 or more alphanum, . or -
\.                  : a dot
[A-Z]{2,6}          : 2 upto 6 alpha (be aware that TLDs may have much more characters, see: https://www.iana.org/domains/root/db)

Result for given example:

grep -Pi "[A-Z0-9][\w.%+-]+(?:@|@|\(at\)|\[at\]|%26%23064%3B|%40)[A-Z0-9][A-Z0-9.-]+\.[A-Z]{2,6}" file.txt

info@example.com
info@example.com
info(at)example.com
info[at]example.com
info%26%23064%3Bexample.com
info%40example.com
  • 1
    @Joao: you have to add -or options so it becomes grep -Pior .... if you want to grep all files recursively and output only the matched part. – Toto Aug 3 '18 at 11:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.