我看了,但没有找到答案。 我想读取一个日志文件并打印出“:”之后的所有内容,但有些日志之前有空格。我想在开头只匹配一个没有空格的那个。
_thisnot: this one has space
thisyes: this one has not space at the beginning.
我想对文件中的每一行都这样做。
答案 0 :(得分:1)
怎么样:
#!/usr/bin/perl
use strict;
use warnings;
use 5.010;
my %result;
while(<DATA>) {
chomp;
next if /^\s/;
if (/^([^:]*):\s*(.*)$/) {
$result{$1} = $2;
}
}
__DATA__
thisnot: this one has space
thisyes: this one has not space at the beginning.
答案 1 :(得分:1)
或者你可以使用像衬里一样的衬里:
perl -ne 's/^\S.*?:\s*// && print' file.log
答案 2 :(得分:0)
# Assuming you opened log filehandle for reading...
foreach my $line (<$filehandle>) {
# You can chomp($line) if you don't want newlines at the end
next if $line =~ /^ /; # Skip stuff that starts with a space
# Use /^\s/ to skip ALL whitespace (tabs etc)
my ($after_first_colon) = ($line =~ /^[^:]*:(.+)/);
print $after_first_colon if $after_first_colon; # False if no colon.
}