【发布时间】:2021-09-26 23:28:53
【问题描述】:
我需要处理 .txt 文件,并按文件名中存储的名称和日期进行过滤。
目前我实现了以下目标:
my $dir = "t-files\/";
chdir($dir);
foreach $files (glob('*.txt')) {
($sname) = split(/_/, $files);
#($sdate) = "still under work"
print "\nSwitch Name: $sname - Date: still under work";
}
文件示例名称:"s-ar-ar55g-1_20140911-09.txt" | "s-ar-ar55g-1_20141027-09.txt" | 等
使用此脚本,我有以下输出:
D:\_perl>test_01.pl
Switch Name: s-ar-ar55g-1 - Date: still under work
Switch Name: s-ar-ar55g-1 - Date: still under work
Switch Name: s-ar-ar55g-1 - Date: still under work
Switch Name: s-ar-ar55g-1 - Date: still under work
Switch Name: s-ar-ar55g-1 - Date: still under work
Switch Name: s-ar-ar55g-1 - Date: still under work
Switch Name: s-ar-ar55g-1 - Date: still under work
Switch Name: s-ar-ar55g-1 - Date: still under work
Switch Name: s-ar-ar55g-1 - Date: still under work
D:\_perl>
我的意图是从文件中提取日期字符串“20140911”,并存储到一个新变量“sdate”中
通过这种方式我需要有两个变量,所以我可以与名称和日期进行比较
是否可以直接从txt文件名中提取像“20140911”这样的年月日?
【问题讨论】: