Objective is to create a bash file to read a given file and output the word count
Read Lines From An File Input
input="path_to_the _text_file"
while IFS= read -r line
do
echo "$line"
done < "$input"
if [[ $str = *main* ]]; then
echo "Yes"
fi
Replace a string with an substring
#!/bin/bash
firstString="I love Suzi and Marry"
secondString="Sara"
echo "${firstString/Suzi/$secondString}"
# prints 'I love Sara and Marry'
#!/bin/bash
count=0
input=~/Documents/sample-text.txt
while read -r line; #reading lines in the while loop
do
for word in $line; #for loop to count words in a line
do
read -ra ARR <<< "$word" #will read $word to an array named ARR
for i in "${ARR[@]}"; do # access each element of array
echo "$i" #to see what are the elements of array
count=$((count+1)) #keeps the count of the words seperated.
done
done
done <$input
echo "c= $count " #displays the final
OUTPUT :
Comments
Post a Comment