简体   繁体   English

读取文件时如何格式化每一行?

[英]How can I format each line while reading a file?

Input format: 输入格式:

2014-09-21 00:09:22,718 TRACE [user: admin12] common.Log (PerformanceExtractor.Python:9776) - ClientId:895,UserId:258,Ip:111.1.1.1,DurationMls:23,DurationString:0.023 seconds,Url:Calculate.LoanExmple

My output would be into variables; 我的输出将变成变量; for example: 例如:

$date = 2014-09-21 00:09:22,718 $user = admin12 $ClientId= 895 $UserID=258 $ip = 111.1.1.1 $time=0.023 $url=Calculate.LoanExmple

In JAVA I would tackle this using a loop, a Stream Object, and a regular expression. 在JAVA中,我将使用循环,流对象和正则表达式解决此问题。 I have no idea how to tackle this using Perl. 我不知道如何使用Perl解决这个问题。 I will also insert this variable as a column into a database and the line will be at least 3000 max 5000 each time i will launch the .pl. 我还将将此变量作为列插入数据库中,并且每次启动.pl时,该行至少应为3000 max 5000。

my loop is 我的循环是

{
print $line;

--formatting here?

last if $. == 500;

}

This just prints out the line as above - I guess the best solution would be to format it and get the values into variables while reading each line, ready to INSERT into DB with DBI libraries. 这只是打印出上面的行-我猜最好的解决方案是格式化它,并在读取每一行时将值转换为变量,以使用DBI库将其插入DB。

any suggestions? 有什么建议么?

Something like this? 像这样吗

while (<$fh>) {
    my @fields = m{^
        (\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:[\d,]+)
        \s TRACE \s
        \[user:\s(\w+)]
        \s common.Log \s \(PerformanceExtractor\.Python\:\d+\) \s - \s
        ClientId:(\d+),
        UserId:(\d+),
        Ip:([\d.]+),
        DurationMls:\d+,
        DurationString:([\d.]+) \s seconds,
        Url:(\S+)
    $}x
        or next;  # skip lines which don't match regexp

    printf('$date=%s; $user=%s; $client_id=%s; $user_id=%s; $ip=%s; $time=%s; $url=%s', @fields);
    print "\n";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM