用于扫描脚本所在文件夹并搜索特定文件的脚本[Perl]

用于扫描脚本所在文件夹并搜索特定文件的脚本[Perl],perl,file,search,directory,Perl,File,Search,Directory,我正在创建一个脚本,在特定文件夹中打开该脚本,其中包含两种不同类型的文件。”安全”(Secure.text、Secure.001.text等)文件和“消息”(message.txt等)文件。每个文件都包含年、日、日期、小时和分钟的日志。我希望能够扫描这些文件,并创建一个新的文件,其中包含2006年1月的所有日志条目。此脚本的目的是只能扫描一组数据(安全),而不是全部数据(消息) 我的脚本在查找当前工作目录时遇到问题。据我所知,perl将使用脚本的位置作为当前目录。我正在使用opendir和clo

我正在创建一个脚本,在特定文件夹中打开该脚本,其中包含两种不同类型的文件。”安全”(Secure.text、Secure.001.text等)文件和“消息”(message.txt等)文件。每个文件都包含年、日、日期、小时和分钟的日志。我希望能够扫描这些文件,并创建一个新的文件,其中包含2006年1月的所有日志条目。此脚本的目的是只能扫描一组数据(安全),而不是全部数据(消息)

我的脚本在查找当前工作目录时遇到问题。据我所知,perl将使用脚本的位置作为当前目录。我正在使用opendir和closedir函数,并得到提示,脚本目录中有60个文件,但当我删除一些文件只是为了测试它时,它仍然显示60,这使我相信这不是在使用当前目录。尽管正则表达式正确,但它也没有找到“安全的”

我得到的输出是“查找文件‘安全’的问题”

#/usr/bin/perl
严格使用;
使用警告;
#找到并扫描所有文件
#列出与此perl脚本位于同一目录中的所有文件。
我的$dirname=“”;
opendir(my$dh,$dirname)#将$dh设置为当前工作目录
或死亡“无法打开目录'$dirname':$!\n”#如果无法打开当前目录,则终止。
我的@all___文件#实例化说明当前目录中所有文件的新变量。
而(my$file=readdir($dh)){#$file则负责设备上的每个文件。
push(@all_the_file,$file);#将当前目录中的文件推送到$file。
}
closedir($dh);
#获取所有安全文件。
我的@all\u\u\u安全文件;
@all\u the\u secure\u file=grep(/^secure(\.\d{1,2})$/,@all\u the\u file);
#对安全文件进行加密。
我的$filename=“安全”;

打开(my$fhin,“你真的应该使用一些模块


未测试,但它应该做+-与您的脚本相同,甚至更多,因为它以递归方式搜索…

脚本的位置不一定是当前工作目录。当前工作目录是当前工作目录。例如,请查看和模块。此外,“迭代安全文件”和打印并创建包含数据的新文件“不实际执行注释所述操作…基本调试:(1)将
$dirname
设置为实际路径(硬编码)(2)您从
opendir
获取的内容没有路径!因此,请将
$dirname
预先添加到
opendir
返回的内容中//然后您将扫描该
$dirname
中的文件//请不要删除要测试的文件!为什么不将它们打印到屏幕上并检查它们是否正确?记住,它们需要有完整的您还可以使用
if(非-f$file){print“No$file\n”}
测试这些字符串(name
$file
)是系统上的实际文件//还有其他问题,但这只是一个开始。如何使当前工作目录成为运行脚本的目录?这基本上是本文的重点,以便我可以更改脚本中文件的名称,并有一个程序为我组织文件。您阅读了我包含的链接吗在我上面的评论中,ded是path(/some/path”);设置文件所在位置的默认路径?@MatthewParise如果“默认路径”是指完整路径,则是。如果必须使用与脚本运行位置相关的路径,则可以使用
FindBin
(和
Cwd
)然后构建它的完整路径,然后你仍然可以使用这个伟大的模块。这个答案提供了你所需要的一切,以及未来使用的蓝图。@MatthewParise嗯…对不起,
path($dir)
/some/path
处为文件/目录构造一个对象。它在这里被分配给
$root
,然后您可以在
$root
上调用模块的方法。
路径()
获取文件/目录的完整路径,这就是
/some/path
的含义(这就是我在上面的评论中的意思)。
#!/usr/bin/perl


use strict;
use warnings;



#Locate and scan all of the files 

 #list all files in same dir as this perl script.
 my $dirname = "."; 
 opendir( my $dh, $dirname ) #sets $dh as the current working directory
    or die "Can't open dir '$dirname':$!\n"; #Kills if can not open current directory.
 my @all_the_file; #Instantiates new variable that accounts for all of the files in the current dir.
 while( my $file = readdir($dh) ) { #$file accounts for every file on device. 
    push( @all_the_file, $file ); #Pushes files in current directory to $file.

 }
 closedir( $dh ); 

 #Gets all of the secure files. 

 my @all_the_secure_file;
 @all_the_secure_file = grep(/^secure(\.\d{1,2})?$/, @all_the_file);

 #Itterate over secure files. 

my $filename = "secure";
open( my $fhin, "<", $filename)
    or die "Can't open '$filename':$!\n";
chomp( my @lines = <$fhin> ) ;
close($fhin);

 #Match the Regex of Jan. 

 my @jan_lines = grep( /^Jan/ , @lines ) ;
print "The file '$filename' = " . @lines . " lines.\n";
print "Size of \@jan_lines = " . @jan_lines . "\n";

 #Print and create new file with Data. 

 my $filename2 = "secure.1";
open( my $fhin, "<", $filename)
    or die "Can't open '$filename':$!\n";
chomp( my @lines2 = <$fhin> ) ;
close($fhin);

my @jan_lines2 = grep( /^Jan/ , @lines2 ) ;
print "The file '$filename2' = " . @lines2 . " lines.\n";
print "Size of \@jan_lines2 = " . @jan_lines2 . "\n";

 exit;
use 5.014;
use warnings;
use Path::Tiny;

my $root = path("/some/path");
my $iter = $root->iterator({recurse => 1});

while(my $path = $iter->()) {
    next unless $path->basename =~ /(secure|message)\.(\d+)?\.txt/i;

    my $type = $1;
    #my $num = $2; #???

    my @lines = grep { /^Jan/ } $path->lines();
    say "in the $path is ", scalar @lines, " lines for Jan";

    my $new = path( ($type =~ /secure/i) ? "/some/where/secure.txt" : "/some/nosecure/message.txt" );
    $new->spew(@lines);
}