巴什格雷普在第一场比赛中停止

  • 本文关键字:一场 雷普 bash
  • 更新时间 :
  • 英文 :

user9@totl.example.com>, size=35020827, class=-30, nrcpts=1, msgid=<2m96JLQblfm/fh.01u3YnFYK0bc3pmOExg2vA.totl.example.com>, proto=ESMTP, daemon=MTA-v6, relay=lemur.totl.example.com
user11@shoe-bags.example.com>, size=18071179, class=-30, nrcpts=1, msgid=<BhaYKoWuhDhrUQcT5.+tF6eKTCu0459KjSflNxLg.shoe-bags.example.com>, proto=ESMTP, daemon=MTA-v6, relay=dog.shoe-bags.example.com
user23@stellar-patrol.example.com>, size=27057917, class=-30, nrcpts=1, msgid=<VaD1xW8SduAYImck.Mbx1MBcKTjBPlQpcaDhJRA.stellar-patrol.example.com>, proto=ESMTP, daemon=MTA-v6, relay=feinstein.stellar-patrol.example.com
user6@planet-express.example.com>, size=15212380, class=-30, nrcpts=1, msgid=<4wN8i90XT.BIdywWoKxNjeEM1q.planet-express.example.com>, proto=ESMTP, daemon=MTA-v6, relay=fry.planet-express.example.com
user19@blackmesa.example.com>, size=44656174, class=-30, nrcpts=1, msgid=<1froj29vndf7h0.Qzoi+1hDEQOVp1frnQvWO.blackmesa.example.com>, proto=ESMTP, daemon=MTA-v6, relay=barney.blackmesa.example.com
user2@stellar-patrol.example.com>, size=4556372, class=-30, nrcpts=1, msgid=<jnugzy+Z.L82rx1mhoSXi0RmK/yNP.stellar-patrol.example.com>, proto=ESMTP, daemon=MTA-v6, relay=feinstein.stellar-patrol.example.com
user7@macrohard.example.com>, size=35391498, class=-30, nrcpts=1, msgid=<fXr7+HM1U7ZpbJqxf.iJs6q9r.macrohard.example.com>, proto=ESMTP, daemon=MTA-v6, relay=corporate-mail-01.macrohard.example.com
user7@lawanda.example.com>, size=46296174, class=-30, nrcpts=1, msgid=<UJHE3Y4uEn.JBT3RESrNYL+fH5dFTGt5A.lawanda.example.com>, proto=ESMTP, daemon=MTA-v6, relay=achilles.lawanda.example.com
user14@feddit.example.com>, size=12197030, class=-30, nrcpts=1, msgid=<gpq6lYSHHC67d.ZjyKUitfcPwOlA/OEc++.feddit.example.com>, proto=ESMTP, daemon=MTA-v6, relay=kittin.feddit.example.com

我希望只提取每行的电子邮件地址部分,例如 user9@tot1.example.com

我目前正在使用这种技术:

cat file | grep -o 'user.*?com'

但是,由于".com"偶尔在行的末尾,因此我仍然以某种方式返回整行。

我的示例输出应如下所示:

user9@totl.example.com
user11@shoe-bags.example.com
user23@stellar-patrol.example.com
... etc

这怎么可能?非常感谢您的帮助

这应该可以:

grep -o 'user[^[:space:]]+.com' file

并观察我在这里不需要cat

这将使用字符类[:space:] 。我要说的是,我想要所有以 user 开头、以 .com 结尾且仅包含非空格字符(和至少一个)介于两者之间([^[:space:]]+)的所有字符。


关于你的解决方案:你需要-P开关,让grep使用 Perl 的正则表达式,以便.*?入为匹配任何内容,不贪婪

grep -Po 'user.*?com' file

会工作。

现在我希望你没有任何客人有电子邮件user42@coolcompagny.com或类似的,否则这个会在这里失败,因为你只会得到user42@coolcom :(

使用正则表达式解析电子邮件地址根本不是一件容易的事。

您可以使用awk来获取该行的一部分。在您的情况下,它将是这样的:

cat file | grep -o 'user.*?com' | awk -F',' '{print $1}'

有关更多功能,您应该查看 GNU Awk 用户指南 http://www.gnu.org/software/gawk/manual/gawk.html

.*? 模式只有在你给 grep -P 选项时才有效,该选项启用 Perl 风格的正则表达式。添加它,它应该可以工作。

最新更新