简体   繁体   English

如何在Perl中使用正则表达式来处理文本

[英]How do you take part of a text using regex in perl

How do you take a part a line in a text file which will be used to reformat the way the file should look? 您如何在文本文件中占一行,该行将用于重新格式化文件外观?

so for example in my text of: 例如,在我的文字中:

Dec 27 21:49:41 osboxes NetworkManager[686]: <info>    nameserver '192.168.221.2'

The regex that I made would match the the text 我制作的正则表达式将与文本匹配

  /([a-z][a-z][a-z]\s(\d\d)\s(\d\d):(\d\d):(\d\d)\s[a-z]*\s(NetworkManager).*)/ig

but how do you make it so that the output will be different, basically rearranging so that it would look something like this 但是如何制作,以便输出会有所不同,基本上是重新排列,以便看起来像这样

**NetworkManager** osboxes Dec 27 21:49:41 <info>    nameserver '192.168.221.2'. [686]

For your usecase, rather than dealing with all those regexes, just use the Parse::Syslog module: it should be more robust and flexible in case you needed to extend your script. 对于您的用例,而不是处理所有这些正则表达式,只需使用Parse :: Syslog模块:如果您需要扩展脚本,它应该更加健壮和灵活。

use strict;
use warnings;
use Parse::Syslog;
my $syslogfile = "syslog.txt";
my $parser = Parse::Syslog->new( $syslogfile , allow_future => 1);
while(my $sl = $parser->next) {
 print "**$sl->{program}** $sl->{host} ".localtime($sl->{timestamp})." $sl->{text} [".$sl->{pid}."]\n"; }

SOmething like this will do. 这样的事情会做。 Notice that we can simplify it a lot, since the date at the beginning will always be 15 characters. 请注意,由于开始日期始终为15个字符,因此我们可以对其进行很多简化。

$b = "Dec 27 21:49:41 osboxes NetworkManager[686]: <info>    nameserver '192.168.221.2'";
$b =~ m/(.{15})\s+([a-z]*)\sNetworkManager(\[\d+\]):\s+(.*)/ig;
# **NetworkManager** osboxes Dec 27 21:49:41 <info>    nameserver '192.168.221.2'. [686]
print( "**NetworkManager** $2 $1 $4 $3\n" );
# prints:
# **NetworkManager** osboxes Dec 27 21:49:41 <info>    nameserver '192.168.221.2' [686]

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

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