使用perl将包含特定已知字符串的文件的内容保存在单个.txt或.tmp文件中
我试图编写一个perl脚本,其中我试图将包含特定字符串“PYAG_GENERATED”的文件的全部内容一个接一个地保存在一个.txt/.tmp文件中。这些文件名采用特定模式,此模式为“output\u nnnn.txt”,其中nnnn为00010002,依此类推。但我不知道有多少文件使用这个“output\u nnnn.txt”名称。 我是perl新手,不知道如何解决这个问题以正确获得输出。有人能帮我吗。提前谢谢 我尝试用不同的方式编写perl脚本,但输出文件中没有任何内容。我在这里给你一个我试过的new_1.txt'是我想要保存预期输出的新文件,生成的PYAG_是我在文件中找到的特定字符串使用perl将包含特定已知字符串的文件的内容保存在单个.txt或.tmp文件中,perl,Perl,我试图编写一个perl脚本,其中我试图将包含特定字符串“PYAG_GENERATED”的文件的全部内容一个接一个地保存在一个.txt/.tmp文件中。这些文件名采用特定模式,此模式为“output\u nnnn.txt”,其中nnnn为00010002,依此类推。但我不知道有多少文件使用这个“output\u nnnn.txt”名称。 我是perl新手,不知道如何解决这个问题以正确获得输出。有人能帮我吗。提前谢谢 我尝试用不同的方式编写perl脚本,但输出文件中没有任何内容。我在这里给你一个我试
open(NEW,">>new_1.txt") or die "could not open:$!";
$find2="PYAG_GENERATED";
$n='0001';
while('output_$n.txt'){
if(/find2/){
print NEW;
}
$n++;
}
close NEW;
我希望输出文件'new_1.txt'将保存文件名模式为'output_nnnn.txt'的文件的全部内容,其中至少有一次包含'PYAG_GENERATED'字符串。我猜您已经尝试过了 欢迎来到Perl的奇妙世界,在这里,做X的方法总是有十几种:-一种可能的方法来实现您想要的。我写了很多评论,希望对大家有所帮助。为了清楚起见,它也有点冗长。我相信它可以被压缩到5行代码
use warnings; # Always start your Perl code with these two lines,
use strict; # and Perl will tell you about possible mistakes
use experimental 'signatures';
use File::Slurp;
# this is a subroutine/function, a block of code that can be called from
# somewhere else. it takes to arguments, that the caller must provide
sub find_in_file( $filename, $what_to_look_for )
{
# the open function opens $filename for reading
# (that's what the "<" means, ">" stands for writing)
# if successfull open will return we will have a "file handle" in the variable $in
# if not open will return false ...
open( my $in, "<", $filename )
or die $!; # ... and the program will exit here. The variable $! will contain the error message
# now we read the file using a loop
# readline will give us the next line in the file
# or something false when there is nothing left to read
while ( my $line = readline($in) )
{
# now we test wether the current line contains what
# we are looking for.
# the index function gives us the index of a string within another string.
# for example index("abc", "c") will give us 3
if ( index( $line, $what_to_look_for ) > 0 )
{
# we found what we were looking for
# so we don't need to keep looking in this file anymore
# so we must first close the file
close( $in );
# and then we indicate to the caller the search was a successfull
# this will immedeatly end the subroutine
return 1;
}
}
# If we arrive here the search was unsuccessful
# so we tell that to the caller
return 0;
}
# Here starts the main program
# First we get a list of files
# we want to look at
my @possible_files = glob( "where/your/files/are/output_*.txt" );
# Here we will store the files that we are interested in, aka that contain PYAG_GENERATED
my @wanted_files;
# and now we can loop over the files and see if they contain what we are looking for
foreach my $filename ( @possible_files )
{
# here we use the function we defined earlier
if ( find_in_file( $filename, "PYAG_GENERATED" ) )
{
# with push we can add things to the end of an array
push @wanted_files, $filename;
}
}
# We are finished searching, now we can start adding the files together
# if we found any
if ( scalar @wanted_files > 0 )
{
# Now we could code that us ourselves, open the files, loop trough them and write out
# line by line. But we make life easy for us and just
# use two functions from the module File::Slurp, which comes with Perl I believe
# If not you have to install it
foreach my $filename ( @wanted_files )
{
append_file( "new_1.txt", read_file( $filename ) );
}
print "Output created from " . (scalar @wanted_files) . " files\n";
}
else
{
print "No input files\n";
}
它起作用了。谢谢。实际上,有几十种方法可以用Perl编写问题代码。下面我给出了我在得到这个解决方案之前做过的另一个代码,它也起了作用。真有趣。
use strict;
use warnings;
my @a;
my $i=1;
my $find1="PYAG_GENERATED";
my $n=1;
my $total_files=47276; #got this no. of files by writing 'ls' command in the terminal
while($n<=$total_files){
open(NEW,"<output_$n.txt") or die "could not open:$!";
my $join=join('',<NEW>);
$a[$i]=$join;
#print "$a[10]";
$n++;
$i++;
}
close NEW;
for($i=1;$i<=$total_files;$i++){
if($a[$i]=~m/$find1/){
open(NEW1,">>new_1.tmp") or die "could not open:$!";
print NEW1 $a[$i];
}
}
close NEW1;