Shell-Script
在 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
如果不需要對數據進行其他處理,那麼您不需要 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
這會將輸入視為
/
-delimited 欄位,並在格式文本字元串中為以 開頭的每一行輸出最後兩個此類欄位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