我有这个perl函数,我想在vmstat之后调用它:
sub insert_datetime{
while($line = <>) {
do
print($line);
if($line =~ /[0-9].*/)
{
`date '+ %m-%d-%Y %H:%M:%S'`;
}
else
{
print("\n")
}
}
}
当我调用vmstat命令时,在perl中,我想在每行中插入日期字段,如下所示:
nohup `vmstat -Iwt 30 2884 | insert_datetime >vmstat_log &`;
我知道你需要在perl中返回unix命令。如果unix命令的输出需要管道进入perl sub怎么办?
答案 0 :(得分:1)
它对火箭科学并不需要太多:
#!/usr/bin/perl
use strict;
use warnings;
use POSIX qw(strftime);
while (<>)
{
chomp;
$_ .= strftime(' %m-%d-%Y %H:%M:%S', localtime(time)) if ($_ !~ m/^\d/);
print "$_\n";
}