【发布时间】:2011-03-05 12:10:42
【问题描述】:
我写了一个 Perl 程序,它从文本文件中读取文本并打印出来。
我想打印出具有特定格式的行。
例如,有一些这样的行:
information:
Ahmad.prn:592118:2001:7:5:/Essay
Ashford.rtf:903615:2001:6:28:/usr/Essay
Barger.doc:243200:2001:7:4:/home/dir
end of Information.
我只想读这三行:
Ahmad.prn:592118:2001:7:5:/Essay
Ashford.rtf:903615:2001:6:28:/usr/Essay
Barger.doc:243200:2001:7:4:/home/dir
我认为字段的含义是:
Ahmad.prn <- file name
592118 <- size of file
2001:7:5 <- created date
/Essay <- path of file
我的代码是这样的:
#!/usr/bin/perl
use strict;
use warnings;
open (my $infh, "<", $file)||die "cant open";
while(my $line = <$infh>) {
chomp ($line);
if ($line =~ /(what regular expression do I have to put in here?)/) {
print "$line";
}
}
close ($infh);
【问题讨论】:
-
您确实需要提供更多关于您要实现的目标的信息。
-
请清楚说明行格式是什么。目前尚不清楚您是否只想删除第一行和最后一行,仅匹配以 /Essay 结尾的行,匹配匹配 a:b:c:d:e:f ....的行。