拆分并存储在 Perl 中

拆分并存储在 Perl 中

我有一个包含以下内容的文件:

Ref  BBPin      r:/WORK/M375FS/HMLSBLK4BIT0P0_0/LSVCC15
Impl BBPin      i:/WORK/M375FS/HMLSBLK4BIT0P0_0/LSVCC15

Ref  BBPin      r:/WORK/HMLSBLK4BIT0P0_0/LSVCC3
Impl BBPin      i:/WORK/HMLSBLK4BIT0P0_0/LSVCC3

Ref  BBPin      r:/WORK/HMLSBLK4BIT0P0_0/LSVSS
Impl BBPin      i:/WORK/HMLSBLK4BIT0P0_0/LSVSS

Ref  BBPin      r:/WORK/IOP054_VINREG5_0/R0T
Impl BBPin      i:/WORK/IOP054_VINREG5_0/R0T

Ref  BBPin      r:/WORK/IOP055_VINREG5_1/R0T
Impl BBPin      i:/WORK/IOP055_VINREG5_1/R0T   

和我正在运行的代码

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

my @words;
my $module;
my $pad;
open(my $file,'<',"file1.txt") or die $!;   
OUT: while(<$file>){
    chomp;
    $pad =$', if($_ =~ /(.*)\//g);

    @words= split('/');
    OUT1: foreach my $word (@words){        
         if($word eq 'IOP054_VINREG5_0'){
             print "Module Found \n";
             $module=$word;last OUT;
         }
    }
}
print $module, "\n";
print ("The pad present in module is :");
print $pad, "\n";

但我想显示所有最后的话。我怎样才能实现这个目标?预期产出

 HMLSBLK4BIT0P0_0 
The pad present in module is LSVCC15
 HMLSBLK4BIT0P0_0
   The pad present in module is LSVCC3
 HMLSBLK4BIT0P0_0
 The pad present in module is LSVSS 
IOP054_VINREG5_0 
The pad present in module is R0T
  IOP054_VINREG5_0 
The pad present in module is R0T

我的代码显示了什么

IOP054_VINREG5_0 
    The pad present in module is R0T

答案1

如果不需要对数据进行其他处理,则不需要 Perl。一个简单的awk脚本就足够了:

$ awk -F '/' '/^Ref/ { printf("%s\nThe pad present in module is %s\n", $(NF-1), $NF) }' file
HMLSBLK4BIT0P0_0
The pad present in module is LSVCC15
HMLSBLK4BIT0P0_0
The pad present in module is LSVCC3
HMLSBLK4BIT0P0_0
The pad present in module is LSVSS
IOP054_VINREG5_0
The pad present in module is R0T
IOP055_VINREG5_1
The pad present in module is R0T

这将输入视为/- 分隔字段,并以 开头的每一行的格式化文本字符串输出最后两个此类字段Ref


使用 Perl:

#!/usr/bin/env perl

use strict;
use warnings;

while (<>) {
    /^Ref/ || next;

    chomp;
    my ($module, $pad) = (split('/'))[-2,-1];

    printf("%s\nThe pad present in module is %s\n", $module, $pad);
}

运行它:

$ perl script.pl file
HMLSBLK4BIT0P0_0
The pad present in module is LSVCC15
HMLSBLK4BIT0P0_0
The pad present in module is LSVCC3
HMLSBLK4BIT0P0_0
The pad present in module is LSVSS
IOP054_VINREG5_0
The pad present in module is R0T
IOP055_VINREG5_1
The pad present in module is R0T

相关内容