用于计算单词并在一个文件中打印的Perl脚本

时间:2014-06-04 09:57:38

标签: perl word-count

我一直在为我的硕士论文编写Perl脚本,从10K(公司的年度报告)中提取一小段文本(CAE)。经过大量的工作,我设法完成了这个脚本的编写工作。现在我需要写一个新的剧本,但由于下周的截止日期,我恐怕不能及时完成。我想知道是否有人可以帮我解决以下问题:

我有大约52.000个.txt文件,带有一小段文字。我需要一个脚本,记下每个.txt文件的名称,以及此文件中的单词和/或字符数量,并将所有文件的副本复制到一个文本文件中。

有人可以帮助我吗?我真的很感激!

这是我到目前为止所得到的:

#!/usr/bin/perl -w
use strict;
use warnings;

my $folder;                     #Base directory for the 10K filings
my $subfolder="2012";           #Subdirectory where 10K filings are placed (Default is ./10K/10K_Raw/2012/*.txt)
my $folder10kcae="10K_CAE";     #Name of subdirectory for output (CAE)
my $folderwc="10K_WC";          #Name of subdirectory for output (WordCount)
my $target_cae;                 #Name of target directory for output (CAE)
my $target_wc;                  #Name of target directory for output (WordCount)
my $slash;                      #Declare slash (dependent on operating system)
my $file;                       #Filename
my @allfiles;                   #All files in directory, put into an array
my $allfiles;                   #Total files in directory
my $data;                       #Input file contents
my $cae;                        #Results of the search query (CAE)
my $wc                          #Results of the search query (WordCount)
my $output_cae;                 #Output file with CAE
my $output_wc;                  #Output file with WordCount
my $log;                        #Log file (also used to determine point to continue progress)
my $logfile="$subfolder".".log";#Filename of log file
my @filesinlog;                 #Files that have been processed according to log file

{
#Set folders for Windows. Put raw 10K filings in folder\subfolder
$slash="\\";
$folder="C:\\10KK\\";                    ###specify correct base-map###
}


#Open source folder and read all files
opendir(DIR,"$folder$slash$subfolder") or die $!;
@allfiles=grep /(.\.txt)/, readdir DIR;
chomp(@allfiles);


#Creates destination folder
$target_wc="$folder$slash$folder10kwc$slash$subfolder";

mkdir "$folder$slash$folder10kwc";
mkdir $target_wc;


#Count lines, words and characters
my ($lines, $words, $chars) = (0,0,0);

while ($data=@allfiles) {
$lines++;
$chars += length($_);
$words += scalar(split(/\s+/, $_));
}

open $output_wc, ">", "$target_wc$slash$file" or die $!;
print $output_wc $wc;
close $output_wc;

print("lines=$lines words=$words chars=$chars\n");

1 个答案:

答案 0 :(得分:2)

我说你在这里有一点轮子改造问题,我不会使用perl脚本。有一个名为' wc'的unix命令行工具。 (字数统计),无需编程即可完成您想做的一切。

在unix上

$ wc /path/to/my/folder/* > /path/to/my/output/file.txt

在Windows上,您可以将wc程序作为GNU Coreutils for Windows包的一部分下载,然后在Windows样式中运行相同的命令

C:\ > wc \path\to\my\folder\* > \path\to\my\output\file.txt