2016-07-19 58 views
1

我想從文件夾中的多個文本文件打印某些行,具體取決於文件名。考慮以下用下劃線分隔的3個單詞命名的文本文件:從多個文本文件中提取特定行

Small_Apple_Red.txt 
Small_Orange_Yellow.txt 
Large_Apple_Green.txt 
Large_Orange_Green.txt 

如何實現以下目標?

if (first word of file name is "Small") { 
    // Print row 3 column 2 of the file (space delimited); 
} 

if (second word of file name is "Orange") { 
    // print row 1 column 4 of the file; 
} 

這可能與awk?

回答

0

請嘗試以下操作。使用glob來處理文件夾中的文件。

然後使用正則表達式檢查文件名。這裏 grep用於從文件中提取特定內容。

my $path = "folderpath"; 
while (my $file = glob("$path/*")) 
{ 
    if($file =~/\/Small_Apple/) 
    { 
     open my $fh, "<", "$file"; 
     print grep{/content what you want/ } <$fh>; 
    } 

} 
+0

這是用於Perl嗎? – amatek

+0

@amatek是的。這是perl – mkHun

0
use strict; 
use warnings; 

my @file_names = ("Small_Apple_Red.txt", 
        "Small_Orange_Yellow.txt", 
        "Large_Apple_Green.txt", 
        "Large_Orange_Green.txt"); 

foreach my $file (@file_names) { 
    if ($file =~ /^Small/){ // "^" marks the begining of the string 
     print "\n $file has the first word small"; 
    } 
    elsif ($file =~ /.*?_Orange/){ // .*? is non-greedy, this means that it matches anything<br> 
            // until the first "_" is found 
     print "\n $file has the second word orange"; 
    } 
} 

仍然有它你的文件有「Small_Orange」你必須決定哪個更重要的一個特例。如果第二個字是更重要的,然後從if部分與內容從elsif部分

0

切換內容在awk中:

在Perl
awk 'FILENAME ~ /^Large/ {print $1,$4} 
    FILENAME ~ /^Small/ {print $3,$2}' * 

perl -naE 'say "$F[0] $F[3]" if $ARGV =~ /^Large/; 
      say "$F[2] $F[1]" if $ARGV =~ /^Small/ ' * 
0

試試這個:

use strict; 
use warnings; 
use Cwd; 
use File::Basename; 

my $dir = getcwd(); #or shift the input values from the user 
my @txtfiles = glob("$dir/*.txt"); 

foreach my $each_txt_file (@txtfiles) 
{ 
    open(DATA, $each_txt_file) || die "reason: $!"; 
    my @allLines = <DATA>; 
    (my $removeExt = $each_txt_file)=~s/\.txt$//g; 
    my($word1, $word2, $word3) = split/\_/, basename $removeExt; #Select the file name with matching case 
    if($word1=~m/small/i) #Select your match case 
    { 
     my @split_space = ""; 
     my @allrows = split /\n/, $allLines[1]; #Mentioned the row number 
     my @allcolns = split /\s/, $allrows[0]; 
     print "\n", $allcolns[1]; #Mentioned the column number 
    } 
} 
相關問題