我有一个像
这样的日志文件field1 field2 field3 field4withvariablelength ... field5with ...
有没有办法使用perl正则表达式提取例如字段5,比如
“用空格分隔”和“给我索引5”??
答案 0 :(得分:5)
当然,您可以使用split按空格分隔:
my (@fields) = split /\s+/;
print $fields[4];
这是一个完整的测试脚本:
#!/usr/bin/perl -w
use strict;
while (<DATA>) {
my (@fields) = split /\s+/;
print $fields[4];
}
__DATA__
field1 field2 field3 field4withvariablelength... field5with...
答案 1 :(得分:0)
或者像这样:
my $str = "field1 field2 field3 field4withvariablelength ... field5with...";
$str =~ m/field5(.*)$/i;
print $1; # type with...
如果你有其他领域:
$str =~ m/\s+field4(.*)\s+/i;
print $1; # type withvariable...