【问题标题】:I have a log file with different columns and from that i need only two columns我有一个包含不同列的日志文件,我只需要两列
【发布时间】:2017-05-08 12:19:31
【问题描述】:

我有一个很大的日志文件,我只需要两列。以下是日志文件中的一行:

[2017-05-02 13:02:25,986] [AJPRequestHandler-HTTPThreadGroup-10239] SID0003457|10.22.165.22|13|16|FAILED|500||<?xml version="1.0" encoding="UTF-8"?> <S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"><S:Header><ns1:SessionType xmlns:ns1="http://tough.example.com/webservices">STATELESS</ns1:SessionType><ns2:UsernameToken xmlns:ns2="http://tough.example.com/webservices">ocore-intg_w w@example.com</ns2:UsernameToken><ns3:PasswordText xmlns:ns3="http://tough.example.com/webservices">UdontsoiuN</ns3:PasswordText></S:Header><S:Body><getExternalPortalUserElement xmlns="http://ws.web. tough1.tough.example.com/"><emailAddress>park.phannon23@example.com</emailAddress></getExternalPortalUserElement></S:Body></S:Envelope>|{http://ws.web.tough1.tough.example.com/}getExternalPortalUserElement|<n s1:Header xmlns:ns1="http://schemas.xmlsoap.org/soap/envelope/"><ns1:SessionType xmlns:ns1="http://tough.example.com/webservices">STATELESS</ns1:SessionType><ns2:UsernameToken xmlns:ns2="http://tough.example.com/webservices">ocore-intg_ww@example.com</ns2:UsernameToken><ns3:PasswordText xmlns:ns3="http://tough.example.com/webservices">UiojdfdoiNN</ns3:PasswordText></ns1:Header>|0|620|null

我只需要一个单独的文件中的输出,其中包含来自日志文件的日期,即第一列和电子邮件地址,就像这样

2017-05-02 13:02:25 park.phannon23@example.com

谁能帮忙,我浏览了多个网站,但无法像上面那样格式化。

【问题讨论】:

  • 向我们展示您的代码。你试过什么?你有什么问题?
  • 欢迎来到 StackOverflow。这不是免费的编码服务。请使用tour 并阅读How to Ask
  • “我只粘贴日志文件中的一行” 但是你贴了四行文字,所以无法猜测原始数据是什么样子的。
  • @Picard 大家都这么说,但从下面的多个答案中可以看出,只要人们迫切需要积分,那显然是。
  • @123:OP 继续同样的事情,没有尝试任何东西,不会发布任何答案。我很确定。

标签: bash perl shell


【解决方案1】:
#!/usr/bin/perl
use strict;
use warnings;

my $data = '[2017-05-02 13:02:25,986] [AJPRequestHandler-HTTPThreadGroup-10239] SID0003457|10.22.165.22|13|16|FAILED|500||<?xml version="1.0" encoding="UTF-8"?> <S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"><S:Header><ns1:SessionType xmlns:ns1="http://tough.example.com/webservices">STATELESS</ns1:SessionType><ns2:UsernameToken xmlns:ns2="http://tough.example.com/webservices">ocore-intg_w
w@example.com</ns2:UsernameToken><ns3:PasswordText xmlns:ns3="http://tough.example.com/webservices">UdontsoiuN</ns3:PasswordText></S:Header><S:Body><getExternalPortalUserElement xmlns="http://ws.web.
tough1.tough.example.com/"><emailAddress>park.phannon23@example.com</emailAddress></getExternalPortalUserElement></S:Body></S:Envelope>|{http://ws.web.tough1.tough.example.com/}getExternalPortalUserElement|<n
s1:Header xmlns:ns1="http://schemas.xmlsoap.org/soap/envelope/"><ns1:SessionType xmlns:ns1="http://tough.example.com/webservices">STATELESS</ns1:SessionType><ns2:UsernameToken xmlns:ns2="http://tough.example.com/webservices">ocore-intg_ww@example.com</ns2:UsernameToken><ns3:PasswordText xmlns:ns3="http://tough.example.com/webservices">UiojdfdoiNN</ns3:PasswordText></ns1:Header>|0|620|null';

if ( $data =~ m!^\[(\d{4}-\d{2}-\d{2})\s+(\d{2}:\d{2}:\d{2}).*<emailAddress>([^<>]*)<\/emailAddress>!gs ){
    print "$1 $2 $3";   
}

输出:2017-05-02 13:02:25 park.phannon23@example.com

Working demo

【讨论】:

  • ([^&lt;&gt;]*) 优于 (.*),以防 XML 中有两个或多个此类元素。
【解决方案2】:

获得预期输出的另一种方法:

my $date_email = "";
while(<DATA>)
{
    my $line = $_;
    $date_email .= "date = $1\t" if($line=~m/^\[([^\[\]]*)\]/m);
    $date_email .= "email = $1\n" if($line=~m/<emailAddress>([^<>]*)<\/emailAddress>/);

}

print $date_email;

__DATA__
[2017-05-02 13:02:25,986] [AJPRequestHandler-HTTPThreadGroup-10239] SID0003457|10.22.165.22|13|16|FAILED|500||<?xml version="1.0" encoding="UTF-8"?> <S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"><S:Header><ns1:SessionType xmlns:ns1="http://tough.example.com/webservices">STATELESS</ns1:SessionType><ns2:UsernameToken xmlns:ns2="http://tough.example.com/webservices">ocore-intg_ww@example.com</ns2:UsernameToken><ns3:PasswordText xmlns:ns3="http://tough.example.com/webservices">UdontsoiuN</ns3:PasswordText></S:Header><S:Body><getExternalPortalUserElement xmlns="http://ws.web.tough1.tough.example.com/"><emailAddress>park.phannon23@example.com</emailAddress></getExternalPortalUserElement></S:Body></S:Envelope>|{http://ws.web.tough1.tough.example.com/}getExternalPortalUserElement|<ns1:Header xmlns:ns1="http://schemas.xmlsoap.org/soap/envelope/"><ns1:SessionType xmlns:ns1="http://tough.example.com/webservices">STATELESS</ns1:SessionType><ns2:UsernameToken xmlns:ns2="http://tough.example.com/webservices">ocore-intg_ww@example.com</ns2:UsernameToken><ns3:PasswordText xmlns:ns3="http://tough.example.com/webservices">UiojdfdoiNN</ns3:PasswordText></ns1:Header>|0|620|null

输出:

date = 2017-05-02 13:02:25,986 email = park.phannon23@example.com

【讨论】:

    【解决方案3】:

    如下使用perl

    $ perl -ne '/\[(.*?)\].*\s+(\S+)\|\{/;print "$1  $2\n";' file1
    2017-05-02 13:02:25,986  w@example.comUdontsoiuNpark.phannon23@example.com
    

    现在是编辑后的例子

    perl -e 'undef $/;$content=<ARGV>;while($content=~/\[(.*?)\].*?<emailAddress>(.*)<\/emailAddress>/sg){print "$1  $2\n";}' file1
    

    【讨论】:

    • 当 OP 要求 2017-05-02 13:02:25 park.phannon23@example.com 时,这有什么用?
    • @Borodin 问题变了,我不只是发明了一些输入吗?考虑一下!!
    • 版本一的问题需要2017-05-02 13:02:25 park.phannon23@example.com的结果
    猜你喜欢
    • 2020-07-09
    • 2011-09-26
    • 2023-04-04
    • 1970-01-01
    • 2020-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-01
    相关资源
    最近更新 更多