首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从任意文件中剥离电子邮件地址

从任意文件中剥离电子邮件地址
EN

Stack Overflow用户
提问于 2013-05-01 00:55:02
回答 2查看 92关注 0票数 0

从大型文件集中获取user@host.com组合的最佳方法是什么?

我假设sed/awk可以做到这一点,但我不太熟悉regexp。

我们有一个文件,即Staff_data.txt,它不仅包含电子邮件,而且希望剥离其余数据,只收集电子邮件地址(即h@south.com)。

我认为最简单的方法是在终端中通过sed/awk,但是考虑到regexp可能有多复杂,我希望得到一些指导。

谢谢。

EN

回答 2

Stack Overflow用户

发布于 2013-05-01 00:56:48

你想在这里使用grep,而不是sedawk。例如,显示来自域south.com的所有电子邮件

代码语言:javascript
复制
grep -o '[^ ]*@south\.com ' file
票数 0
EN

Stack Overflow用户

发布于 2013-05-01 02:10:00

这是我几年前为完成这项工作而写的一个有点尴尬但显然可以工作的脚本:

代码语言:javascript
复制
# Get rid of any Message-Id line like this:
#   Message-ID: <AANLkTinSDG_dySv_oy_7jWBD=QWiHUMpSEFtE-cxP6Y1@mail.gmail.com>
#
# Change any character that can't be in an email address to a space.
#
# Print just the character strings that look like email addresses.
#
# Drop anything with multple "@"s and change any domain names (i.e.
# the part after the "@") to all lower case as those are not case-sensitive.
#
# If we have a local mail box part (i.e. the part before the "@") that's
# a mix of upper/lower and another that's all lower, keep them both. Ditto
# for multiple versions of mixed case since we don't know which is correct.
#
# Sort uniquely.

cat "$@" |
awk '!/^Message-ID:/' |
awk '{gsub(/[^-_.@[:alnum:]]+/," ")}1' |
awk '{for (i=1;i<=NF;i++) if ($i ~ /.+@.+[.][[:alpha:]]+$/) print $i}' |
awk '
  BEGIN   { FS=OFS="@" }
  NF != 2 { printf "Badly formatted %s skipped.\n",$0 | "cat>&2"; next }
  { $2=tolower($2); print }
' |
tr '[A-Z]' '[a-z]' |
sort -u

它不是很漂亮,但看起来很健壮。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16305155

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档