我有一個陣列@files,其中有多個.txt檔案。我正試圖從這些檔案中的行開始,貪污所有具有"/home/[a-z] "模式的行。我正在嘗試以下方法:
my @lines = grep (/^/home/[a-z] /, @files) 。
chomp(@lines)。
my @line = uniq(@lines)。
但是當我試圖列印@lines時,我在輸出中沒有得到任何東西。
誰能告訴我,我在grep命令中做錯了什么。謝謝。
uj5u.com熱心網友回復:
請看下面的代碼樣本,它演示了你選擇的一種方法。
如果你能提供一個內容@files陣列的樣本,將會非常有幫助。
注意:沒有必要使用uniq(@lines),因為檔案系統在其組織中假定為uniq檔案名
use strict;
use warnings;
use feature 'say';
my @files = <DATA> 。
my @lines = grep( m!^/home/[a-z] /!, @files);
say @lines;
__DATA__
/home/alex/work/samples
/home/philip/doc/asterix
/home/maria/bin/quick_search.c
/bin/grep[/span]。
/sbin/test
輸出
/home/alex/work/Samples
/home/philip/doc/asterix
/home/maria/bin/quick_search.c
示例代碼用于過濾存盤在陣列@files中的檔案串列的匹配模式。
use strict;
use warnings;
use feature 'say';
my @files = qw/abc.txt, bcd.txt, cde.txt/;
my @lines;
for my $file(@files) {
my @temp;
open my $fh, '< ' , $file
or die "Couldn't open $file" ;
@temp = grep( m!^/home/[a-z] /!, <$fh> );
close $fh;
@lines = (@lines, @temp);
}
chomp(@lines)。
say for @lines;
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/311383.html
標籤:
下一篇:應用呼叫javaFunction<?擴展UpboundType,Integer>物件并且不能將確切的UpboundType物件傳遞給apply()
