如何在linux中使用正则表达式匹配多个电子邮件地址?

如何在linux中使用正则表达式匹配多个电子邮件地址?
[email protected]
[email protected]
[email protected]

如何匹配这些电子邮件地址?任何以 island.ac.kr 结尾的电子邮件地址

答案1

您可以使用以下正则表达式:

.*@island\.ac\.kr$

例子:

$ cat /tmp/111
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
$ grep '.*@island\.ac\.kr$' /tmp/111
[email protected]
[email protected]
[email protected]
[email protected]

符号后面的点@需要转义。

答案2

使用awk并假设输入每行包含一个电子邮件地址。

awk -F @ 'tolower($NF) == "island.ac.kr"' input.txt

或者,如果您希望始终输出小写字母,

awk -F @ 'tolower($NF) == "island.ac.kr" { print tolower($0) }' input.txt

这将每一行视为一组@- 分隔的字段。当最后一个字段 ( $NF) 全部小写时,恰好是island.ac.kr,则打印该行。

测试:

$ cat input.txt
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
$ awk -F @ 'tolower($NF) == "island.ac.kr"' input.txt
[email protected]
[email protected]
[email protected]
[email protected]

如果您还想匹配类似的地址[email protected],您可以awk像这样使用:

awk -F @ 'tolower($NF) ~ /island\.ac\.kr$/'

或者,与greptr

tr '[:upper:]' '[:lower:]' <input.txt | grep 'island\.ac\.kr$'

wheretr用于将所有字符折叠为小写,然后grep用于挑选出相关行。

要不就

grep -i 'island\.ac\.kr$' input.txt

如果您不介意可能获得混合大小写输出。

相关内容