-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile_operations.sh
More file actions
41 lines (33 loc) · 1.08 KB
/
file_operations.sh
File metadata and controls
41 lines (33 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#!/bin/bash
# Check if a filename is provided
if [ -z "$1" ]; then
echo "Error: No file specified."
echo "Usage: $0 <filename>"
exit 1
fi
# Check if the file exists
if [ ! -f "$1" ]; then
echo "Error: File '$1' does not exist."
exit 1
fi
filename="$1"
# Task a: Count the number of characters, words, and lines
char_count=$(wc -m < "$filename")
word_count=$(wc -w < "$filename")
line_count=$(wc -l < "$filename")
echo "Task a: Count of characters, words, and lines:"
echo "Characters: $char_count"
echo "Words: $word_count"
echo "Lines: $line_count"
# Task b: File in reverse order
echo "Task b: File in reverse order:"
tac "$filename"
# Task c: Frequency of a particular word
echo "Task c: Frequency of a particular word"
echo "Enter the word to count its frequency:"
read word
word_freq=$(grep -o -i "\b$word\b" "$filename" | wc -l)
echo "The word '$word' appears $word_freq times in the file."
# Task d: Replace lower case alphabets in place of upper-case alphabets
echo "Task d: Replacing upper-case alphabets with lower-case alphabets:"
tr '[:upper:]' '[:lower:]' < "$filename"