[英]Extract specific lines from multiple text files
我想从文件夹中的多个文本文件中打印某些行,具体取决于文件名。 考虑以下由下划线分隔的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?
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
节
在Awk中:
awk 'FILENAME ~ /^Large/ {print $1,$4}
FILENAME ~ /^Small/ {print $3,$2}' *
在Perl中:
perl -naE 'say "$F[0] $F[3]" if $ARGV =~ /^Large/;
say "$F[2] $F[1]" if $ARGV =~ /^Small/ ' *
试试这个:
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
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.