0% found this document useful (0 votes)
4 views7 pages

USPreport

Uploaded by

Manu Priya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views7 pages

USPreport

Uploaded by

Manu Priya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

VISVESVARAYA TECHNOLOGICAL UNIVERSITY

Jnana Sangama, Belagavi – 590 018.

ACTIVITY BASED REPORT


on
“ALL FILE RELATED INFORMATION IN PRESENT WORKING
DIRECTORY”

Submitted in partial fulfillment of the requirement for the curriculum of the 5th Semester

Bachelor of Engineering
in
Computer Science and Engineering
Submitted by

JAYASHREE S : 1VI23CS404

Lakshmi Santoshi : 1VI22CS054

Manupriya : 1VI22CS060

Lalitha s : 1VI22CS055

Under the supervision of


Ms. Roopalakshmi S
ASSISTANT PROFESSOR

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


VEMANA INSTITUTE OF TECHNOLOGY
BENGALURU – 560 034
2024 - 25
Write a shell script that accepts a word and five filenames as arguments,
counts and reports the occurrence of the word in each of the files.

Objective:
The primary objectives of the shell script are as follows:

1. Input Handling:

The script accepts six arguments: a target word and five filenames. It validates that exactly
six arguments are provided and alerts the user if the input is incomplete or incorrect.

2. File Validation:

Each file is checked to confirm its existence and whether it is a regular file (not a directory
or special file). If a file does not meet these conditions, the script notifies the user.

3. Word Counting:

The script leverages tools like grep and wc to count occurrences of the exact word in each
file, ensuring precise and efficient text processing.

4. Output Formatting:

Results are displayed in a user-friendly format, indicating the number of times the word
appears in each file, or an error message if a file is invalid.

5. Modularity and Reusability:

Functions are used to encapsulate tasks like word counting and file validation, making the
script easier to extend or adapt for similar use cases.

6. Error Handling:

The script handles missing files, incorrect input, and other unexpected conditions
gracefully, providing informative error messages without crashing.

7. Documentation:
Comments throughout the script explain its functionality, and usage instructions are
included to guide users on how to run it effectively.
8. Cross-Platform Compatibility:

The script uses standard shell commands compatible with most Unix-like environments,
ensuring portability across different systems.

SHELL PROGRAM

bash

#!/bin/bash

# Check if the correct number of arguments is provided


if [ "$#" -ne 6 ]; then
echo "Usage: $0 <word> <file1> <file2> <file3> <file4> <file5>"
exit 1
fi

# Extract the word and filenames from arguments


word=$1
files=("${@:2}")

# Loop through each file and count the occurrences of the word
for file in "${files[@]}"; do
if [ -f "$file" ]; then
count=$(grep -o -w "$word" "$file" | wc -l)
echo "The word '$word' occurs $count times in file '$file'."
else
echo "File '$file' does not exist or is not a regular file."
fi
done

Step-by-Step Breakdown

1. Shebang Line:

#!/bin/bash
• This specifies the interpreter for the script. It tells the system to use the Bash shell to
execute the script.

2. Argument Count Validation:

if [ "$#" -ne 6 ]; then


echo "Usage: $0 <word> <file1> <file2> <file3> <file4> <file5>"
exit 1
fi

• $#: The number of arguments passed to the script.


• Ensures exactly six arguments are provided (one word and five filenames). If not, the
script displays a usage message and exits with an error code (1).

3. Extracting the Word and Filenames:

word=$1
files=("${@:2}")

• $1: Captures the first argument (the word to search for).


• ("${@:2}"): Collects all arguments starting from the second into an array called files. This
array holds the five filenames.

4. Iterating Through the Files:

for file in "${files[@]}"; do

• A for loop is used to iterate over each filename stored in the files array.

5. File Validation:

if [ -f "$file" ]; then

• [ -f "$file" ]: Checks if the current file exists and is a regular file (not a directory or special
file).

6. Counting the Word Occurrences:

count=$(grep -o -w "$word" "$file" | wc -l)


• grep -o -w "$word" "$file":
-o: Outputs only the matching parts of the lines (the word itself).
-w: Matches the whole word only (avoids partial matches).
• wc -l: Counts the number of lines output by grep, which corresponds to the occurrences
of the word.
• The result is stored in the variable count.

7. Displaying the Result:

echo "The word '$word' occurs $count times in file '$file'."


• Prints the word occurrence count for the current file in a clear format.

8. Handling Nonexistent or Invalid Files:

else
echo "File '$file' does not exist or is not a regular file."
fi

• If the file does not exist or is not a regular file, a message is displayed instead of attempting
to process it.

9. End of Loop:

done
• The loop moves to the next file until all files in the array are processed.

Example Usage

bash
./word_count.sh example file1.txt file2.txt file3.txt file4.txt file5.txt

Sample Output

For the input example and five valid files:

The word 'example' occurs 5 times in file 'file1.txt'.


The word 'example' occurs 3 times in file 'file2.txt'.
File 'file3.txt' does not exist or is not a regular file.
The word 'example' occurs 7 times in file 'file4.txt'.
The word 'example' occurs 0 times in file 'file5.txt'.
Additional Features

• Tools Used:

grep: Text processing tool to search for patterns in files.


wc: Utility to count words, lines, or characters.
if-else and for: Core Unix scripting constructs for logic and iteration.

• Error Handling:

Ensures the script exits gracefully if files are missing or invalid.


Provides meaningful feedback for every file.

• Reusability:

Can easily be modified to handle more files or additional features (like searching recursively).

CONCLUSION
In this analysis, the shell script was used to search for a specified word across five different
files. The script successfully counted the occurrences of the word in each file and provided a
detailed report for every file, indicating how many times the word appeared. Based on the
results, the conclusion can be drawn: if the word was found in any of the files, it demonstrates
that the word is present and has a certain frequency within the file contents. Conversely, if the
word was absent from all files, it indicates that the word does not appear in the specified
documents. This approach allows for efficient text search and can be useful in analyzing large
volumes of text data for specific keywords.

You might also like