[英]How to read multiple files from a directory, extract specific strings and ouput to an html file?
Greetings, 问候,
I have the following code and am stuck on how I would proceed to modify it so it will ask for the directory, read all files in the directory, then extract specific strings and ouput to an html file? 我有以下代码,并停留在如何继续进行修改的过程中,这样它将询问目录,读取目录中的所有文件,然后提取特定的字符串并输出到html文件中? Thanks in advance.
提前致谢。
#!/usr/local/bin/perl
use warnings;
use strict;
use Cwd;
print "Enter filename: "; # Should be Enter directory
my $perlfile =STDIN;
open INPUT_FILE, $perlfile || die "Could not open file: $!";
open OUTPUT, '>out.html' || die "Could not open file: $!";
# Evaluates the file and imports it into an array.
my @comment_array = ;
close(INPUT_FILE);
chomp @comment_array;
@comment_array = grep /^\s*#/g, @comment_array;
my $comment;
foreach $comment (@comment_array) {
$comment =~ /####/; #Pattern match to grab only #s
# Prints comments to screen
Print results in html format
# Writes comments to output.html
Writes results to html file
}
close (OUTPUT);
Take it one step at a time. 一次迈出一步。 You have a lot planned, but so far you haven't even changed your prompt string to ask for a directory.
您有很多计划,但是到目前为止,您甚至都没有更改提示字符串来查询目录。
To read the entered directory name, your: 要读取输入的目录名称,请执行以下操作:
my $perlfile =STDIN;
gives an error (under use strict;
). 给出错误(
use strict;
)。 Start by looking that error up ( use diagnostics;
automates this) and trying to figure out what you should be doing instead. 首先查找该错误(
use diagnostics;
自动执行此操作),然后尝试找出您应该做什么。
Once you can prompt for a directory name and print it out, then add code to open the directory and read the directory. 一旦您可以提示输入目录名称并打印出来,然后添加代码以打开目录并读取目录。 Directories can be opened and read with
opendir
and readdir
. 可以使用
opendir
和readdir
打开和读取目录。 Make sure you can read the directory and print out the filenames before going on to the next step. 在继续下一步之前,请确保您可以阅读目录并打印出文件名。
a good starting point to learn about specific functions (from the cmd line) 一个学习特定功能的好起点(从cmd行)
perldoc -f opendir
However, your particular problem is answered as follows, you can also use command line programs and pipe them into a string to simplify file handling ('cat') and pattern matching ('grep'). 但是,您的特定问题的回答如下,您还可以使用命令行程序并将它们通过管道传递给字符串,以简化文件处理('cat')和模式匹配('grep')。
#!/usr/bin/perl -w
use strict;
my $dir = "/tmp";
my $dh;
my @patterns;
my $file;
opendir($dh,$dir);
while ($file = readdir($dh)){
if (-f "$dir/$file"){
my $string = `cat $dir/$file | grep pattern123`;
push @patterns, $string;
}
}
closedir($dh);
my $html = join("<br>",@patterns);
open F, ">out.html";
print F $html;
close F;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.