Write a Perl script that counts the number of times a particular word appears in a text file.

GPT 4o

Article cover image

\n#!/usr/bin/perl\n\nuse strict;\nuse warnings;\n\n# Define the word to search\nmy $search_word = "example";\n\n# Open the file for reading\nopen(my $fh, "<", "input.txt") or die "Could not open file: $!";\n\n# Initialize the counter\nmy $count = 0;\n\n# Read the file line by line\nwhile (my $line = <$fh>) {\n # Split the line into words\n my @words = split(/\\s+/, $line);\n\n # Loop through the words and count the occurrences of the search word\n foreach my $word (@words) {\n if ($word eq $search_word) {\n $count++;\n }\n }\n}\n\n# Close the file handle\nclose($fh);\n\n# Print the result\nprint "The word '$search_word' appears $count times in the file.\\n";\n\n\nNote: Replace "example" with the word you want to search for and "input.txt" with the name of your text file

Write a Perl script that takes a text file as input and prints the number of lines that contain the string "Perl". ...Read more
Write a Perl script to find all the unique words in a text file and then display the number of occurrences of each word. ...Read more
Write a Fortran program that reads the contents of a text file and counts the number of characters, words, and lines in the file. ...Read more
I'm curious, how does an object move in a vacuum? ...Read more
Write a Perl script that searches a text file for a specific string of characters. ...Read more
Write a Perl script to read a file line by line and print the lines that contain a specific word. ...Read more
Write a shell script that takes a text file as an argument and counts the number of lines, words, and characters in the file. ...Read more
Write a Perl script that searches for a given string in a text file and displays the line number ...Read more
Write a PHP script that reads data from a text file line by line using file handling and count occurrences of each word in a file. ...Read more
Write a Perl script to count the number of lines in a given file that contain a specific word. ...Read more
A text file contains various words and numbers. Write a Lua script to read the file and count the number of times each word appears in the file. ...Read more
Write a Perl script to count the number of occurrences of each word in a given text file. ...Read more
Write a Perl program to count the number of times a given word appears in a text file. ...Read more
Write a COBOL program that uses the TCP/IP protocol to send a message from one computer to another over a network. ...Read more
How would you write a Perl script to count the number of times a word appears in a text file? ...Read more
Write a Fortran program to read the contents of a text file line by line and count the number of lines with a specific word or pattern. ...Read more
Write a Perl script to find the longest word in a given text file. ...Read more
Write a Perl script to find all the unique words in a given text file and count how many times each word appears. ...Read more
Write a Perl script that reads lines of text from a file, counts the number of lines in the file, and then prints the line count to the console. ...Read more
Use Unix shell to write a program that counts the number of lines in a given text file. ...Read more
Create a Perl script that reads lines from a text file, then sorts and outputs them back to the file. ...Read more
Write a Perl script that reads the contents of a text file and counts the number of lines containing the word "love" in it. ...Read more
Write a Visual Basic code to open a text file titled "Sample.txt" that is located on the desktop and read the third line of the file. ...Read more
Write a Perl script that searches a text file for a specific string, and outputs the line number and location of each occurrence. ...Read more
Write a Perl script that takes a text file as input and counts the occurrence of each word in the file. The script should then print the words and their respective counts in descending order ...Read more
Write a Perl script that takes a text file as input and counts the number of words and characters in the file. ...Read more
Write a real-life story shared by someone in a reddit forum. The story should be somehow related to this text snippet: “How can I use PHP to extract the legal drinking age for each state in ...Read more
How do I turn a Apps Script to Appsheet? ...Read more