Код perl для подсчета количества слов в текстовом файле
Я написал код для подсчета количества слов в текстовом файле и количества строк. но теперь я хочу, чтобы вывод отображал количество слов в каждом файле. например, если входной файл
hello there- 2
i am one of those-5
мой код до сих пор
open FILE, "<editnlp.txt" or die "Cannot read $filename: $!\n";
$lines = 0;
$words = 0;
$letters = 0;
while ( $line = <FILE> ) {
@words = split( " ", $line );
$nwords = @words;
for ( $i = 0 ; $i < $nwords ; $i = $i + 1 ) {
@letters = split( "", $words[$i] );
$nletters = @letters;
$letters = $letters + $nletters;
}
$words = $words + $nwords;
$lines = $lines + 1;
}
print "$filename contains $lines lines, $words words " . "and $letters letters.\n";
Он хорошо работает для подсчета количества слов во всем текстовом файле, но не может редактировать его для подсчета общего количества слов в каждой строке.
3 ответа
Вы можете попробовать этот код. Он напечатает количество слов для каждой строки и, наконец, после завершения всего файла он напечатает общее количество слов и общее количество строк в файле.
# Counting number of words in a files
open(DATA, "+<file1.txt") or die "Couldn't open file file1.txt, $!";
$lines = 0;
$words = 0;
$nwords = 0;
$total = 0;
print "\n";
while ( $line = <DATA> ) {
$lines=$lines+1;
@words = split( " ", $line );
$nwords = @words;
print "Number of words on the line $lines are : $nwords \n";
$total = $total+$nwords;
}
print "\nTotal no. of words in file are $total \n";
print "\nTotal no. of lines in file are $lines \n";
вывод будет выглядеть примерно так, как показано ниже.
Number of words on the line 1 are : 10
Number of words on the line 2 are : 5
Number of words on the line 3 are : 0
Number of words on the line 4 are : 8
Number of words on the line 5 are : 0
Number of words on the line 6 are : 0
Number of words on the line 7 are : 10
Number of words on the line 8 are : 0
Number of words on the line 9 are : 0
Number of words on the line 10 are : 0
Number of words on the line 11 are : 0
Number of words on the line 12 are : 0
Number of words on the line 13 are : 10
Number of words on the line 14 are : 7
Number of words on the line 15 are : 0
Number of words on the line 16 are : 8
Total no. of words in file are 58
Total no. of lines in file are 16
#!/usr/bin/perl
sub linux_style_count {
$filename = shift;
$lines = `wc -l < $filename`;
$words = `wc -w < $filename`;
$chars = `wc -c < $filename`;
print "\nFile $filename contains";
print "\nNumber of Lines = $lines";
print "Number of Words = $words";
print "Number of Chars = $chars\n";
}
sub perl_style_count {
$filename = shift;
open(FILE, "<$filename") or die "Could not open file: $!";
my ($lines, $words, $chars) = (0,0,0);
while (<FILE>) {
$lines++;
$chars += length($_);
$words += scalar(split(/\s+/, $_));
}
print "\nFile $filename contains";
print "\nNumber of Lines = " . $lines;
print "\nNumber of Words = " . $words;
print "\nNumber of Chars = " . $chars . "\n";
}
@files = <*>;
foreach $file (@files) {
if (-f $file) {
&linux_style_count($file);
&perl_style_count($file);
}
}
ВЫХОД
[root@localhost /]# perl count.pl
File mytext.txt contains
Number of Lines = 9
Number of Words = 22
Number of Chars = 214
File mytext.txt contains
Number of Lines = 9
Number of Words = 22
Number of Chars = 214
File hello.txt contains
Number of Lines = 41
Number of Words = 127
Number of Chars = 888
File hello.txt contains
Number of Lines = 41
Number of Words = 127
Number of Chars = 888
File config.ini contains
Number of Lines = 32
Number of Words = 58
Number of Chars = 538
File config.ini contains
Number of Lines = 32
Number of Words = 58
Number of Chars = 538
Или используйте "wc", он работает аналогично команде unix с тем же именем.
увидеть это