我尝试使用perl解析有关用户上次修改目录的信息。
但是,我只对以" U"开头的用户感兴趣。或" u"是否可以使用REGEX删除不以" u"开头的每一行。或" U"?
$ VAR1 =' rachel 512 2014-09-22 10:45:40.584528000 +0100
$ VAR1 =' bob 512 2012-08-15 11:32:17.000773000 +0100
$ VAR1 =' connel 512 2014-07-23 17:43:54.988200000 +0100
$ VAR1 =' jim 512 2014-09-25 14:54:21.080627000 +0100
$ VAR1 =' jackson 512 2014-08-21 11:14:35.686864000 +0100
$ VAR1 =' mel 512 2014-08-19 12:01:30.773249000 +0100
$ VAR1 =' sam 512 2014-09-25 15:44:06.764874000 +0100
$ VAR1 =' u08805 512 2014-01-04 12:16:47.358653000 +0000
$ VAR1 =' u08828 512 2014-09-23 08:30:11.000794000 +0100
$ VAR1 =' u21491 512 2014-07-09 16:04:48.101059000 +0100
$ VAR1 =' u23303 512 2014-09-25 14:33:05.044120000 +0100
$ VAR1 =' u23841 512 2014-08-15 03:15:29.317542000 +0100
$ VAR1 =' u23920 512 2014-07-23 09:42:35.211670000 +0100
这就是我目前所拥有的,我已经必须拆分并使用REGEX来进入这个阶段。
my %timestamp;
$count = 0;
$stdout = $ssh->capture("ls -ladE /export/home/*");
@lines = split(/\n/,$stdout);
foreach(@lines)
{
my @firstsplit = split(/drwxr-x---/,$_);
$firstsplit[1] =~s/^\s+|\s+$//g; #removes whitespace
$firstsplit[1] =~ s/^\S+\s*//; #removes first number from string
}
答案 0 :(得分:4)
目前还不是很清楚你在问什么,但是这个简单的程序演示了如何只提取以u
或U
开头的行:
#!/usr/bin/env perl
while(<>) {
if (/^[uU]/) { print; }
# alternate line: print if /^u/i;
}
用法:
$ ./extract.pl < textfile
u08805 512 2014-01-04 12:16:47.358653000 +0000
u08828 512 2014-09-23 08:30:11.000794000 +0100
...
您可能希望扩展您的模式以包含前导空格:
while(<>) {
if (/^\s*(u.*)/i) { print $1, "\n"; }
}
答案 1 :(得分:3)
要过滤掉以u
或U
开头的数组的每个字符串,请使用
@a = grep { /^[uU]/ } @a;
答案 2 :(得分:1)
这样的事情也可能有用。
# s/(?mi)(?:^(?!u).*\r?\n)+//g
(?mi)
(?:
^
(?! u )
.* \n
)+
Perl:
use strict;
use warnings;
$/ = undef;
my $logdata = <DATA>;
$logdata =~ s/(?i)(?:^(?!u).*\n)+//mg;
print $logdata;
__DATA__
rachel 512 2014-09-22 10:45:40.584528000 +0100
bob 512 2012-08-15 11:32:17.000773000 +0100
connel 512 2014-07-23 17:43:54.988200000 +0100
jim 512 2014-09-25 14:54:21.080627000 +0100
jackson 512 2014-08-21 11:14:35.686864000 +0100
mel 512 2014-08-19 12:01:30.773249000 +0100
sam 512 2014-09-25 15:44:06.764874000 +0100
u08805 512 2014-01-04 12:16:47.358653000 +0000
u08828 512 2014-09-23 08:30:11.000794000 +0100
U21491 512 2014-07-09 16:04:48.101059000 +0100
u23303 512 2014-09-25 14:33:05.044120000 +0100
jim 512 2014-09-25 14:54:21.080627000 +0100
u23841 512 2014-08-15 03:15:29.317542000 +0100
u23920 512 2014-07-23 09:42:35.211670000 +0100
答案 3 :(得分:1)
最好你可以试试这对你有很大的帮助....
use strict;
use warnings;
my $string = qq('uachel 512 2014-09-22 10:45:40.584528000 +0100
'bob 512 2012-08-15 11:32:17.000773000 +0100
'connel 512 2014-07-23 17:43:54.988200000 +0100
'jim 512 2014-09-25 14:54:21.080627000 +0100
'jackson 512 2014-08-21 11:14:35.686864000 +0100
'mel 512 2014-08-19 12:01:30.773249000 +0100
'sam 512 2014-09-25 15:44:06.764874000 +0100
'u08805 512 2014-01-04 12:16:47.358653000 +0000
'u08828 512 2014-09-23 08:30:11.000794000 +0100
'u21491 512 2014-07-09 16:04:48.101059000 +0100
'u23303 512 2014-09-25 14:33:05.044120000 +0100
'u23841 512 2014-08-15 03:15:29.317542000 +0100
'u23920 512 2014-07-23 09:42:35.211670000 +0100);
$string =~ s{^((?:(?!'u).)+)}{}isgm;
print $string;exit;