Upload files to "OS/bash/Week4"

Signed-off-by: Aadit Agrawal <tech@aaditagrawal.com>
This commit is contained in:
Aadit Agrawal 2025-01-24 10:06:30 +05:30
parent 3d5dfd6481
commit 5b0eda8b4e
5 changed files with 108 additions and 0 deletions

38
OS/bash/Week4/q5.sh Normal file
View file

@ -0,0 +1,38 @@
#!/bin/bash
num_patterns=$(( $# ))
patterns=("${@:1:num_patterns}")
input_file="${!num_patterns}"
while true;do
echo "a. Search the patterns in the given input file."
echo "b. Delete all occurrences of the pattern in the given input file."
echo "c. Exit"
read choice
case "$choice" in
a)
echo "Searching patterns in $input_file:"
for pattern in "${patterns[@]}"; do
grep "$pattern" "$input_file"
done
;;
b)
echo "Deleting occurrences of patterns in $input_file:"
for pattern in "${patterns[@]}"; do
sed -i "s/$pattern//g" "$input_file"
done
echo "Occurrences deleted."
;;
c)
echo "Exiting..."
exit 0
;;
*)
echo "Invalid choice. Please enter 'a', 'b', or 'c'."
;;
esac
done