Question

PLEASE WRITE A BASH SCRIPT FOR USE IN JUYPTER NOTEBOOK. Initialize a variable INPUT_FILE that stores...

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

Homework Answers

Answer #1

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
Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
In UNIX .Write a bash script that takes a list of usernames as its command line...
In UNIX .Write a bash script that takes a list of usernames as its command line arguments and displays on the screen, for each user name, a message of the form Number of times that logged into this machine is where is to be replaced by the number of recors that the last command output that match exactly. For example, if i enter command logincount mark it should output something like number of times that sweiss logged into this machin...
COMPLETE IN C++ Declare and initialize a global constant named SIZE with the value 50. Write...
COMPLETE IN C++ Declare and initialize a global constant named SIZE with the value 50. Write a void function called count that accepts two parameters-- a C-string called str representing a C-string passed to the function and an array of integers called alphabets to store the count of the letters of the C-string. Note that the alphabets array stores 26 counters – one for each letter of the alphabet. Use the same counter for lowercase and uppercase characters. The first...
Bash script with branching and looping Part 1: 1. Create a variable called NAME and set...
Bash script with branching and looping Part 1: 1. Create a variable called NAME and set it equal to the empty string (NAME=””). Create a variable called NAME_LENGTH and set it equal to the length of your last name. Output the value of the NAME variable to show that it is empty. 2. Write a FOR loop that executes as many times as there are letters in your name (use the NAME_LENGTH variable in your condition). Use a CASE statement...
write a script named print_lines.sh that uses head and tail together to print out a specific...
write a script named print_lines.sh that uses head and tail together to print out a specific set of lines from a file. The script should take three arguments: the line number to start at, the line number to stop at, and the file to use. Here's an example run: [user@localhost ~]$ print_lines.sh 7 10 /etc/passwd shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown halt:x:7:0:halt:/sbin:/sbin/halt mail:x:8:12:mail:/var/spool/mail:/sbin/nologin operator:x:11:0:operator:/root:/sbin/nologin [user@localhost ~]$ In this example, the script prints line 7 through 10 (inclusive) of the /etc/passwd file. Your script must do...
Write UNIX commands to perform the following tasks. Each task must use exactly one line of...
Write UNIX commands to perform the following tasks. Each task must use exactly one line of command. Unless stated otherwise, all files are assumed to be at your current working directory. a) (10 points) Print your current working directory into a file named file1 b) (10 points) Assume that your current working directory is NOT the same as your home directory. Print all file and subdirectory names of your home directory into a file named file2. c) (15 points) Copy...
The first script you need to write is login.sh, a simple script that you might run...
The first script you need to write is login.sh, a simple script that you might run when you first log in to a machine. We'll expand this script later, but for now it should include your name, username, hostname, current directory, and the current time. Here is some sample output to help guide you. Note that the bolded lines that start with "[user@localhost ~]$" are the terminal prompts where you type in a command, not part of the script. Of...
please write the code in java so it can run on jGRASP import java.util.Scanner; 2 import...
please write the code in java so it can run on jGRASP import java.util.Scanner; 2 import java.io.*; //This imports input and output (io) classes that we use 3 //to read and write to files. The * is the wildcard that will 4 //make all of the io classes available if I need them 5 //It saves me from having to import each io class separately. 6 /** 7 This program reads numbers from a file, calculates the 8 mean (average)...
Arrays, loops, functions: Lotto Element Repeated Function Write a function that that takes as parameters an...
Arrays, loops, functions: Lotto Element Repeated Function Write a function that that takes as parameters an array of ints, an int value named element, and an int value named end. Return a bool based on whether the element appears in the array starting from index 0 and up to but not including the end index. Generate Random Array Write a function that takes as parameters an array of integers and another integer for the size of the array. Create a...
In PYTHON please: Write a function named word_stats that accepts as its parameter a string holding...
In PYTHON please: Write a function named word_stats that accepts as its parameter a string holding a file name, opens that file and reads its contents as a sequence of words, and produces a particular group of statistics about the input. You should report the total number of words (as an integer) and the average word length (as an un-rounded number). For example, suppose the file tobe.txt contains the following text: To be or not to be, that is the...
THIS IS FOR JAVA I have to write a method for a game of Hangman. The...
THIS IS FOR JAVA I have to write a method for a game of Hangman. The word the user is trying to guess is made up of hashtags like so " ###### " If the user guesses a letter correctly then that letter is revealed on the hashtags like so "##e##e##" If the user guesses incorrectly then it increments an int variable named count " ++count; " String guessWord(String guess,String word, String pound) In this method, you compare the character(letter)...