Count the occurrence of a string in an input file
The classic awk solution is something like:
$ awk 'NF{ count[ toupper( $0 ) ]++}
END{ for ( name in count ) { print name " appears " count[ name ] " times" };
}' input
Counting the occurrence of characters within a text file in Java
You are initializing variable count
in every iteration, which is wrong. Declare variable outside of while
and check it will work. From your code it will print only the last line count.
Please do following change to your code:
public class LetterCounter
{
public static void main( String[] args ) throws FileNotFoundException
{
Scanner input = new Scanner( new File( "src/para1.txt" ) );
char someChar = 'a';
int count = 0;
while ( input.hasNextLine() )
{
String answer = input.nextLine();
answer = answer.toLowerCase();
for ( int i = 0; i < answer.length(); i++ )
{
if ( answer.charAt( i ) == someChar )
{
count++;
}
}
System.out.println( answer );
}
System.out.println( "a - " + count );
input.close();
}
}
File handling and counting of occurrence of a string in files
I would also definitely recommend using the count method. From your example, I can not really see where you try to write results to your output file, so I will explain a possible implementation.
def occurrences(inputFileNames, words, outputFileName):
wordCount = {}
# This dictionary will hold our wordCount and be used for construnction of the output file
for file in inputFileNames:
# Iterate over the files
try:
with open(file, 'r') as infile:
content = infile.read().strip().split(" ")
# Declare entry to wordCount for file only if no IOError is raised
wordCount[file] = [0 for j in range(len(words))]
for i in range(len(words)):
# Instead of iterating over the contents manually, split them and use the count method
wordCount[file][i] = str(content.count(words[i]))
except IOError:
print("The file {} could not be read.".format(file))
with open(outputFileName, 'w+') as outfile:
# Iterate over the wordCount dict and write the output
for i in wordCount.keys():
outfile.write(i+" "+" ".join(wordCount[i])+"\n")
occurrences(["book.txt"], ["Alice", "hole", "Rabbit"], "occ.txt")
occ.txt then contains:
book.txt 155 0 26
To do this without the count method, one possible way would be to iterate over the content list element by element and to increment the count if the word matches the element.
for i in range(len(words)):
count = 0
for word in content:
if words[i] == word:
count += 1
wordCount[file][i] = str(count)
When counting the occurrence of a string in a file, my code does not count the very first word
If you want a simple fix it is simple in this line:
wordcount=line.count(' '+item+' ')
There is no space before "This".
I think the are a couple ways to fix it but I recommend using the with
block and using .readlines()
I recommend using some more of pythons capabilities. In this case, a couple recommendations. One if the file is more than one line this code won't work. Also if a sentence is words... lastwordofsentence.Firstwordofnextsentence
it won't work because they will be next to each other and become one word. Please change your replace to do spaces by that i mean change ''
to ' '
, as split will replace multiple spaces .
Also, please post whether you are using Python 2.7 or 3.X. It helps with small possible syntax problems.
filename = input('Enter the name of the file you wish to open: ')
# Using a with block like this is cleaner and nicer than try catch
with open(filename, "r") as f:
all_lines = f.readlines()
d={} # Create empty dictionary
# Iterate through all lines in file
for line in all_lines:
# Replace periods and commas with spaces
line=line.replace('.',' ')
line=line.replace(',',' ')
# Get all words on this line
words_in_this_line = line.split() # Split into all words
# Iterate through all words
for word in words_in_this_line:
#Check if word already exists in dictionary
if word in d: # Word exists increment count
d[word] += 1
else: #Word doesn't exist, add it with count 1
d[word] = 1
# Print all words with frequency of occurrence in file
for i in d.items():
print(i)
Count the occurrences of a string in a file
Close, but a few issues:
use strict;
use warnings;
sub countModule
{
my $file = "/test";
open my $fh, "<",$file or die "could not open $file: $!";
my @contents = <$fh>; # The <> brackets are used to read from $fh.
my @filtered = grep (/\// ,@contents);
return @filtered; # Remove the reference.
}
my @lines = countModule();
my $count = scalar @lines; # 'scalar' is not required, but lends clarity.
print "###########\n $count \n###########\n";
Each of the changes I made to your code are annotated with a #comment explaining what was done.
Now in list context your subroutine will return the filtered lines. In scalar context it will return a count of how many lines were filtered.
You did also mention find the occurrences of a character (despite everything in your script being line-oriented). Perhaps your counter sub would look like this:
sub file_tallies{
my $file = '/test';
open my $fh, '<', $file or die $!;
my $count;
my $lines;
while( <$fh> ) {
$lines++;
$count += $_ =~ tr[\/][\/];
}
return ( $lines, $count );
}
my( $line_count, $slash_count ) = file_tallies();
How do I count the number of occurrences of a string in an entire file?
Using perl's "Eskimo kiss" operator with the -n
switch to print a total at the end. Use \Q...\E
to ignore any meta characters.
perl -lnwe '$a+=()=/\Q(*)/g; }{ print $a;' file.txt
Script:
use strict;
use warnings;
my $count;
my $text = shift;
while (<>) {
$count += () = /\Q$text/g;
}
print "$count\n";
Usage:
perl script.pl "(*)" file.txt
Counting number of occurrence of a string in a text file
what you need is a dictionary.
dictionary = {}
for line in table:
for animal in line:
if animal in dictionary:
dictionary[animal] += 1
else:
dictionary[animal] = 1
for animal, occurences in dictionary.items():
print(animal, ':', occurences)
Count occurrences of character in file -C
There are multiple problems in your code:
ch
must be defined asint
to handleEOF
reliably.while (!feof(fp))
is incorrect. Learn Why is “while ( !feof (file) )” always wrong? You should instead write:while ((ch - getc(fp)) != EOF)
strcmp(&ch, ":")
is incorrect because&ch
is not a C string. You should usech == ':'
instead.you forget to close the file, causing a resource leak.
Here is a modified version:
int NumColon(const char *filename) {
FILE *fp = fopen(filename, "r");
if (fp == NULL) {
return -1;
}
int count = 0;
int c;
while ((c = getc(fp)) != EOF) {
count += (c == ':');
}
fclose(fp);
return count;
}
Related Topics
Unix/Linux Ipc: Reading from a Pipe. How to Know Length of Data at Runtime
Building /Lib/Modules/$(Uname -R)/Build While Compiling a Kernel
I Need a Binary Comparison Tool for Win/Linux
Sending Snmp2 Trap Message from Linux Command Lne
Touch Command Create Multiple Files (Different Names) Under One Directory
How to Send Email from The Command Line
How to Use 9-Bit Serial Communication in Linux
Error Hh604: Error Running JSON-Rpc Server: Error:0308010C:Digital Envelope Routines::Unsupported
Where M Flag and O Flag Will Be Stored in Linux
Where Is G_Multi Configured in Beaglebone Black
How to Run .Exe Executable File from Linux Command Line
Nasm X86_64 Assembly in 32-Bit Mode: Why Does This Instruction Produce Rip-Relative Addressing Code
Dreaming of Making My Own Os- What Should I Use? (Suggestions)
Shared Libraries (Dlopen) and Thread-Safety of Library Static Pointers
Linux Configuration - Ssmtp: Cannot Open Smtp.Gmail.Com:587