从文件中提取一种模式

普拉布

我有一个大文件,其中包含类似于以下所示的日志。我想找到所有受错误影响的交易(TR#)。我需要提取每个TR#ID的一次出现。

我该怎么办?

    Apr 30 16:51:29.574 application.crit: [6104]:TR#14. Transaction send can not be sent. Error Code: 704
    Apr 30 16:51:29.574 application.crit: [6104]:TR#14. Transaction send can not be sent. Error Code: 704
    Apr 30 16:51:29.574 application.crit: [6104]:TR#14. Transaction send can not be sent. Error Code: 704
    Apr 30 16:51:29.574 application.crit: [6104]:TR#14. Transaction send can not be sent. Error Code: 704
    Apr 30 16:51:29.574 application.crit: [6104]:TR#238. Transaction send can not be sent. Error Code: 704
    Apr 30 16:51:29.574 application.crit: [6104]:TR#238. Transaction send can not be sent. Error Code: 704
    Apr 30 16:51:29.574 application.crit: [6104]:TR#238. Transaction send can not be sent. Error Code: 704
    Apr 30 16:51:29.574 application.crit: [6104]:TR#238. Transaction send can not be sent. Error Code: 704
    Apr 30 16:51:29.574 application.crit: [6104]:TR#238. Transaction send can not be sent. Error Code: 704
    Apr 30 16:51:29.574 application.crit: [6104]:TR#238. Transaction send can not be sent. Error Code: 704

要求的输出:

    Apr 30 16:51:29.574 application.crit: [6104]:TR#14. Transaction send can not be sent. Error Code: 704
    Apr 30 16:51:29.574 application.crit: [6104]:TR#238. Transaction send can not be sent. Error Code: 704
芒登

这是非常简单的操作awk

$ awk 'c[$5]++==1' file 
Apr 30 16:51:29.574 application.crit: [6104]:TR#14. Transaction send can not be sent. Error Code: 704
Apr 30 16:51:29.574 application.crit: [6104]:TR#238. Transaction send can not be sent. Error Code: 704

或者,在Perl中:

$ perl -ane '$k{$F[4]}++==1 && print' file 
Apr 30 16:51:29.574 application.crit: [6104]:TR#14. Transaction send can not be sent. Error Code: 704
Apr 30 16:51:29.574 application.crit: [6104]:TR#238. Transaction send can not be sent. Error Code: 704

上面假设每个数字之前的数字TR#ID都是ID的一部分。如果数字可以更改,但您只需要其中之一,请改用此方法:

$ awk -F'[:.]' 'c[$7]++==1' file 

或者

$ perl -F'[:.]' -ane '$k{$F[6]}++==1 && print' file 

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

一种有趣的模式

一种将通知排队的模式?

C ++ 11、14或17是否提供一种仅从decltype()中提取参数的方法?

从Bigquery的时间戳中提取日期:一种更好的方法

一种从具有最小元素值的元组列表中提取元组的优雅方法

grep多列一种模式

从文件中提取一种热编码到数据集中

有没有一种方法可以从文本文件中带括号的数字中提取值?

在spaCy中,是否有一种方法可以提取从中提取实体的句子?

使用grep和regex从仅包含一种元音的文件中提取单词

是否有一种策略(除了反演之外)从假设中提取含义?

有没有一种方法可以在matplotlib中提取画线的像素坐标

从多个CSV文件中提取几种模式

从列中提取一种模式,并在R数据帧中创建一个新模式

有没有一种简短的方法可以从`Vec`中提取一个元素?

从模式中提取文件中每一行的文本

复制符合一种模式但不符合另一种模式的文件

MySQL搜索组合的一种模式?

如何仅从存档中提取一种文件?

有没有一种方法可以从WebException中提取消息?

有没有一种方法可以在View Rails中提取URI或URL?

有没有一种方法可以在View Rails中提取URI或URL?

一种优雅的单行解决方案,可从divmod的嵌套元组中提取数据

是否可以创建一种从文件中提取文本的通用方法?

从巨大的(强制)文本文件中提取两种模式之间的数据

如何将文件从一种递增模式重命名为另一种?

如何在powershell中的foreach循环之前从文本文件中提取一种模式

从猫鼬模式文件中提取模式

这是一种从js中的xmlhttp2请求中提取var的方法吗?