
我使用的是 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