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

これは入力を/区切られたフィールドとして扱い、最後の2つのフィールドを各行を開始するフォーマットされたテキスト文字列として出力しますRef


パールの使用:

#!/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

おすすめ記事