PLEASE WRITE A BASH SCRIPT FOR USE IN JUYPTER NOTEBOOK.
Initialize a variable INPUT_FILE that stores the name of the file 'testdata.csv'.
Write an if-else statement in a shell that checks if the input file exists.
If the file exists, then display a message "testdata.csv exists," otherwise display a message "testdata.csv does not exist."
Initialize an array variable named 'ARR_WORDS' as below: ARR_WORDS=("naresh" "sam" " david" )
Initialize an array variable named 'ARR_COUNTS' of size equal to the length of array ARR_WORDS, and fill the array with an integer value of 0. This is a parallel array we use to place the count of each word in the ARR_WORDS. Initially, we have set all counts to 0.
Use a while looping structure to read the CSV file testdata.csv line by line. In the while block, check if any words in ARR_WORDS is in the line, and increment the corresponding counter value in the array ARR_COUNTS.
Display the result of counts of each word in the ARR_WORDS in a table as below:
#name | count
1. naresh | 12
2. sam | 5
3. David | 4 4. ..
PLEASE INCLUDE CODE AND ANSWER. THANKS
Please find the requested script below. Also including the screenshot of sample output.
Please provide your feedback
Thanks and Happy learning!
Jupyter Output:
output:
Input file format used for testing
%%bash
#Above line sets the environment for the script to bash
#Initialize a variable INPUT_FILE that stores the name of the file 'testdata.csv'.
INPUT_FILE="testdata.csv"
#Write an if-else statement in a shell that checks if the input file exists.
if [ -f ./$INPUT_FILE ]
then
#If the file exists, then display a message "testdata.csv exists,"
echo "$INPUT_FILE exists."
else
#otherwise display a message "testdata.csv does not exist."
echo "$INPUT_FILE does not exist."
fi
#Initialize an array variable named 'ARR_WORDS' as below: ARR_WORDS=("naresh" "sam" " david" )
ARR_WORDS=("naresh" "sam" "david")
#Initialize an array variable named 'ARR_COUNTS' of size equal to the length of array ARR_WORDS.
#and fill the array with an integer value of 0
for i in "${ARR_WORDS[@]}"
do
ARR_COUNTS+=(0)
done
#Use a while looping structure to read the CSV file testdata.csv line by line.
while read line
do
counter=0;
for name in "${ARR_WORDS[@]}"
do
#In the while block, check if any words in ARR_WORDS is in the line
if [ "$name" == "$line" ]
then
#increment the corresponding counter value in the array ARR_COUNTS
count=${ARR_COUNTS[$counter]}
ARR_COUNTS[$counter]=`expr $count + 1`
fi
counter=`expr $counter + 1`
done
done <"./$INPUT_FILE"
#Display the result of counts of each word in the ARR_WORDS in a table as below:
index=0;
echo "#name | count"
for name in "${ARR_WORDS[@]}"
do
sno=`expr $index + 1`
echo "$sno. $name | ${ARR_COUNTS[$index]}"
index=`expr $index + 1`
done
Get Answers For Free
Most questions answered within 1 hours.