Я работал над Perl-скриптом для своей магистерской диссертации, чтобы извлечь небольшой фрагмент текста (CAE) из 10K (годовой отчет компании). Мне удалось закончить написание этого скрипта после большой работы. Теперь мне нужно написать новый сценарий, но из-за дедлайна на следующей неделе я боюсь не успеть закончить. Мне было интересно, если есть кто-то, кто может помочь мне со следующей проблемой:
У меня есть почти 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");
find . -name '*.txt' -exec wc {} \; > file
? - person Biffen   schedule 04.06.2014