Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Part 3 Fill in the blanks. 1) ( The following shell script myUniq is used to cre

ID: 3741001 • Letter: P

Question

Part 3 Fill in the blanks.
1) ( The following shell script myUniq is used to create a frequency list of every word
in a file. When running the shell script, the first argument should be the pathname of the file
to be checked.
Assume we have a file as below:
This is a test
Test test test
There are multiple tests
After running the shell script, a two-column list is displayed on the screen. The first column
shows what words appear, the second column shows how often they appear, for example:
$./myUniq.sh test
test@3
tests@1
multiple@1
is@1
are@1
a@1
This@1
There@1
Test@1
The lines are sorted by the frequency.
cat $1 | tr ' ' ' ' > temp # put all words to a new line
echo -n > file2.txt # clear file2.txt
for line in _______A._______ # trace each line from temp file
do
# check if the current line is visited
grep -q _______B._______ file2.txt
if [_______C._______]
then
count=`_______D.___________` #count the number of words
echo $line"@"$count >> file2.txt # add word and frequency to file
fi
done
sort _______E._______ file2.txt # sort the lines according to the
frequency

Explanation / Answer

Hi,

Check the below snippet which contains full code for given problem

cat $1 | tr ' ' ' ' > temp # put all words to a new line
echo -n > file2.txt # clear file2.txt
for line in $(cat temp) # trace each line from temp file
do
# check if the current line is visited
grep -q $line file2.txt
if [ $? -ne 0 ]
then
count=`grep $line temp -o | wc -l` #count the number of words
echo $line"@"$count >> file2.txt # add word and frequency to file
fi
done
sort -nr file2.txt # sort the lines according to the frequency

Let me break down for ease of understanding

A. $(cat temp) : Reading every line from content of given input file

B. $line : Checking whether this word is present in our output file already. -q is used for silent execution of grep , so that status of grep is stored in $? which we'll use in blank C . If $? == 0, then match is found else match not found.

C. $? -ne 0 : if no match found. i.e if word not already processed for output..

D. grep $line temp -o | wc -l : calculating occurrences of word in entire file. and giving the value to count

E. -nr : Sort the line by number (Numeric Sort)

Hope this helps. Reache me out for doubts if any.

All the best.