从文件中读取一行并将该行拆分为perl

时间:2016-12-14 06:43:14

标签: perl

我必须从文本文件中读取特定行(行包含is successfully created.),并且必须拆分行并将特定值存储在变量中。

整行是

LOG----Payrun.c:0263:28/11/16 07:45:04 > Pay file /home/user/dev/MODULE447/input/all/20161111/PAY001.TXT is successfully created.

我必须将字符串值/home/user/dev/MODULE447/input/all/20161111/放入一个变量,将PAY001.TXT放入另一个变量。

任何人都可以帮我阅读文件并将所需的值输入变量吗?

2 个答案:

答案 0 :(得分:1)

这样的事情会做到这一点。

#!/usr/bin/env perl
use strict;
use warnings;

#iterate stdin or files specified on command line. 
while ( <> ) {
   #regex match, capturing part of the match. 
   #note - non whitespace matching, so will break if you've whitespace chars in your filenames 
   #or paths. 
   if ( my ( $path, $file ) = m,\s(\S+/)(\S+) is successfully created, ) {
      print "Path = $path, file = $file","\n";
   }
} 

答案 1 :(得分:0)

  

我已经为你开始了这个:

use strict;
use warnings;

my $inputfile = $ARGV[0];
my $matchedline = "";

#open file
open(INPUT, $inputfile) || die "Can't reason $!\n";
while(<INPUT>) #Each line looping
{  $matchedline .= $_ if($_=~m/is successfully created/g);  } #Check matched line

print $matchedline; #Confirm the matched line.
  

在此之后,您将尝试存储在新文件中,这是您的作业