我正在使用 Linux Centos 6-64。
我怎样才能更改以下命令,仅检索输入文件夹内以 @ 编写的电子邮件:
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"
检索以下所有 @ 格式的电子邮件?
info@example.com
[email protected]
info(at)example.com
info[at]example.com
info%26%23064%3Bexample.com
info%40example.com
如果可能的话,命令在一行中。谢谢
答案1
您可以使用-P
标志来表示 perl 正则表达式和-i
不区分大小写:
grep -Pi "[A-Z0-9][\w.%+-]+(?:@|@|\(at\)|\[at\]|%26%23064%3B|%40)[A-Z0-9][A-Z0-9.-]+\.[A-Z]{2,6}"
\w
代表[a-zA-Z0-9_]
解释:
[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)
给定示例的结果:
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
[email protected]
info(at)example.com
info[at]example.com
info%26%23064%3Bexample.com
info%40example.com