如何在perl中用匹配模式逐行提取

时间:2017-09-06 19:46:00

标签: perl

我是perl的新手。我正在尝试使用下面的代码在我的文本文件 some : "https://firebasestorage.googleapis.com/v0/b/blackjack-ef580.appspot.com/o/profiles_images%2FdwMGR8sKMvYauf6uQDtyop18cwy1.jpg?alt=media&token=5df48794-d3fd-4e5c-a72c-9928a8a43c4e\"" 中包含字符串“length”的行之后提取行。我想得到如下所示的结果。有人可以帮我解决我需要添加到的内容我的代码。谢谢

merge.txt

my @array; While (@array = ‘/Users/Desktop/merged.txt’) { foreach my $line (@array) { if ($line =~ m/length/) { my $a = $array[$i+1]; push (@array, $a); } my $wanted_type = "$1"; print "$wanted_type\n"; } } } 文件有

merge.txt

我想要的结果如下所示。我也想从行中获取文件名  它有owner@owner-HP-Z840-Workstation ~/SLM/Desktop/Documents % ./count-smRNAs.pl /media/owner/c92ed9e9-3d94-497c-bb2e-514a4806bbcd/merged.fastq 32 32 length number A C G T 32 14824945 1992856 1576607 2413263 8756583 owner@owner-HP-Z840-Workstation ~/SLM/Desktop/Documents % ./count-smRNAs.pl /media/owner/c92ed9e9-3d94-497c-bb2e-514a4806bbcd/merged.fastq 33 33 length number A C G T 33 58619575 1415093 3274505 5499169 48070172 owner@owner-HP-Z840-Workstation ~/SLM/Desktop/Documents % ./count-smRNAs.pl /media/owner/c92ed9e9-3d94-497c-bb2e-514a4806bbcd/test.fastq 34 34 length number A C G T 34 13018196 1047476 903554 1695778 9296236 ,如下所示。

/c92ed9e9-3d94-497c-bb2e-514a4806bbcd/

1 个答案:

答案 0 :(得分:1)

my $print;
while (<>) {
   if ( my $qfn = /^\s*%\s*\S+\s+(\S+)/ ) {
      ( my $fn = $qfn ) =~ s{^.*/}{}s;
      print("$fn\n");
   }

   print if $print;
   $print = /length/;
}