简体   繁体   English

如何从目录中读取多个文件,提取特定的字符串并输出到html文件?

[英]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 . 可以使用opendirreaddir打开和读取目​​录。 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.

相关问题 如何使用File :: Tail从同一目录中读取多个文件? - How to read multiple files from the same directory using File::Tail? 从目录中的多个文本文件中提取特定数据 - Extract specific data from multiple text files in a directory 读取目录中的多个文件并与另一个文件进行比较 - Read multiple files in a directory and compare with another file 如何从不同文件中提取特定列并在一个文件中输出? - How to extract specific columns from different files and output in one file? 从多个文本文件中提取特定行 - Extract specific lines from multiple text files 如何从文件名中提取 4 个字母并在多个文件中替换使用 - How to extract 4 letters from file names and use in substitution in multiple files 使用 grep 从二进制文件中提取非常特定的字符串 - Using grep to extract very specific strings from binary file 从一个目录中的多个pdb文件中解析和提取信息 - Parsing and extract information from multiple pdb files in one directory 如何使用Perl的HTML :: TableExtract从HTML文件中提取带有标题名称的特定列 - How can I extract specific columns with header names from an HTML file using Perl's HTML::TableExtract 如何读取目录中的所有文本文件并输出为html页面的链接 - how to read all text files in a directory and output as links to a html page
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM