【问题标题】:Filter textual data and count records corresponding to a specific value [closed]过滤文本数据并计算与特定值对应的记录[关闭]
【发布时间】:2015-05-19 14:39:20
【问题描述】:

从下面的数据中,我想知道有多少唯一的“Over quota”行对应于每行开头的 ID 值(例如 38148141CEB*),每个 ID 应该只即使有多个“超额”行,也只计算一次。哪种数据结构最适合用于此任务?如何转换数据以实现这一目标?

示例输入

38148141CEB*   55166 Tue May 19 20:38:45  aaa@bbb.com
                                     aaa@nwegweg.com

AABFF141D0F*    6289 Tue May 19 20:38:44  MAILER-DAEMON
                                     rgerg@rgerger.com

3F384141CAF*   15422 Tue May 19 20:38:44  MAILER-DAEMON
                                     gregrg@gserhweh.com

6E5F1142129    59842 Tue May 19 17:22:39  rhgregr@grehedh.in
(connect to mail.rtherh.com.hk[118.142.113.41]:25: Connection timed out)
                                     hbed@herer.com.hk

64ACF141E4D     7026 Tue May 19 14:21:14  hdtjndtrj@tjrftjnrf.com
(host hergherhe.herherh.com[172.16.12.160] said: 452 4.2.2 Over quota (in      reply to end of DATA command))
                                     c@rhedhed.com
(host hergherhe.herherh.com[172.16.12.160] said: 452 4.2.2 Over quota (in reply to end of DATA command))
                                     c@rhedhed.com

67E1A1420CF     2993 Tue May 19 12:36:45  erhejhe
(host z.rherh.com[172.16.12.164] said: 452 4.2.2 Over quota (in reply to end of DATA command))
                                     heehedh88@drehnjedthnj.com
(host hergherhe.herherh.com[172.16.12.160] said: 452 4.2.2 Over quota (in reply to end of DATA command))
                                     c@rhedhed.com

680911418C9    11041 Tue May 19 13:59:49  efswegrwe
(host zgewsegws.nrhsrhedr.com[172.16.12.161] said: 452 4.2.2 Over quota (in reply to end of DATA command))
                                     wrhwrg@etheherg.com

666611416DD    29459 Tue May 19 11:35:58  erherhe@ejerthjrth.com
(host herer.herwsrh.com[172.16.12.152] said: 452 4.2.2 Over quota (in reply to end of DATA command))
                                     rggerhed@erhjehjedtr.com
(host hergherhe.herherh.com[172.16.12.160] said: 452 4.2.2 Over quota (in reply to end of DATA command))
                                     c@rhedhed.com
(host hergherhe.herherh.com[172.16.12.160] said: 452 4.2.2 Over quota (in reply to end of DATA command))
                                     c@rhedhed.com

643F9142006    11077 Tue May 19 11:34:32  tejeastj@eherjhe.com
(Host or domain name not found. Name service error for name=tjedtjedt.com type=MX: Host not found, try again)
                                     tedjedth@edtjedthghr.com

62980141A8C   464040 Tue May 19 15:52:06  etjertj@jrtjedtj.com.hk
(host zherherh.com[172.16.12.159] said: 452 4.2.2 Over quota (in reply to end of DATA command))
                                     trjerjer@rherherh.com

6DD47142017    20049 Tue May 19 11:04:34  erhedrhje
(host ejedtjedrth.com[172.16.12.161] said: 452 4.2.2 Over quota (in reply to end of DATA command))
                                     ehjdthjdr@etjtdfteh.com

696AD1420A4     7679 Tue May 19 15:09:05  wrgwsrg
(host rwherhe.hre.com[172.16.12.157] said: 452 4.2.2 Over quota (in reply to end of DATA command))
                                     wegw@gwrhbwrg.com

样本输出

我需要的输出如下所示:

7 Over quota:
64ACF141E4D, 67E1A1420CF, 680911418C9, 666611416DD, 62980141A8C, 6DD47142017, 696AD1420A4

【问题讨论】:

  • 您是否尝试过解决此问题?如果有,请出示。
  • 嗨。 Stack Overflow 是一个帮助人们解决编程问题的网站。这不是编码服务。这些是存在的,但通常它们要花钱。如果您需要有关编码问题的帮助,我建议您查看How to Ask 并相应地重新起草您的问题。
  • 你没有甚至分享这个“配额”的价值是什么。
  • 哎呀...我有点像push @over 我猜:-P @mak-on-in ...如果您想继续成为 Stackoverflow 的一部分,您应该尝试改善你的问题;更清楚地制定您的要求;有一个清晰且相关的标题/主题,将您的问题与一些通用编程问题/技术联系起来;并至少尝试来解决它(在这种情况下使用 perl)。

标签: perl data-structures perl-data-structures


【解决方案1】:

您可以将数据转换为数组数组,然后遍历数组元素以检查“Over quota”语句。

这一行将split 输入空行 (split/\n{2,}/) 并使用 push 将结果记录放入匿名数组的数组 (@quotalog) 中(注意匿名数组构造函数 @987654327 @):

 perl -n0E 'push @quotalog, map { s/\s+/ /gr ; [split] } split/\n{2,}/ ;
            for (@quotalog){ say $_->[0] if ("@$_" =~ /Over/) }' quotadata.txt

输出

64ACF141E4D
67E1A1420CF
680911418C9
666611416DD
62980141A8C
6DD47142017
696AD1420A4

或者,近似您的示例输出:

perl -n0E 'push @quotalog, map { [split] } split/\n{2,}/ ; 
   for (@quotalog){ push @over, $_->[0] if ("@$_" =~ /Over/) }
   print ~~@over, " Over quota:\n" ; print join ", ", @over,"\n";' quotadata.txt

输出

7 Over quota: 
64ACF141E4D, 67E1A1420CF, 680911418C9, 666611416DD, 62980141A8C, 6DD47142017, 696AD1420A4,

您可能希望将数据转换为数组的散列。在这里,我们使用“double map to a hash”来创建哈希并使用Data::Dumper 来查看它(作为替代方案,您可以使用Data::Printer):

 perl -MData::Dumper -n0E 'map { $quotas{$_->[0]} = [ @{ $_ }[1...$#$_] ] } 
         map { [ split ] } split/\n{2,}/ ; print Dumper \%quotas' quotadata.txt

这仅用于说明目的 - 第二个mapsplit 记录列表中看到的第一个map)可能使用正则表达式而不是简单的@987654339 更好@。在任何情况下,一旦您的数据被很好地安排,您就可以开始从中提取信息 - 尽管更长的脚本表单应用程序会更合适。

在您进行实验时,在The Perl Data Structures Cookbook (cf. perdoc perldsc) 上打开一个终端或浏览器窗口,您将通过相对简单的练习学到很多东西。

【讨论】:

  • 感谢您的帮助!!!
猜你喜欢
  • 1970-01-01
  • 2016-04-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-13
  • 1970-01-01
  • 2014-10-06
  • 1970-01-01
相关资源
最近更新 更多