Linux 1. Create and save a new text file called Gasoline that consists of the fo
ID: 3775895 • Letter: L
Question
Linux
1. Create and save a new text file called Gasoline that consists of the following content: Gas prices stayed about the same the past two weeks, continuing an unusual 12 week trend of steadily increasing prices followed by prices relaxing. The average price for gas nationwide, including all grades and taxes, was about $2.97 a gallon on Friday, according to a survey of 8,000 stations released Sunday. That was up .06 cents per gallon from Jan 18. Prices have been holding steady since early January, when a gallon of gas cost about $3.00.
2. Create an analysis script called TestScript that completes the following tasks for the Gasoline file:
a. Remove punctuation
b. Make all characters lowercase
c. Put each word on a single line
d. Remove blank lines
e. Sort the text to put all lines containing the same word on adjacent lines. Hint: Use | sort | uniq -c for this part.
f. Remove duplicate words from the text
g. List most-used words in the file first
h. Send the output of this script to a file named ScriptResult
3. Save the script. Make it executable. Run the script.
4. Open the ScriptResult file and verify that the contents match what was asked for in the TestScript script.
Explanation / Answer
#################
# TestScript.sh #
##################
#!/bin/bash
printf " -------------- Removed Blank Lines ---------------" > ScriptResult
val3=$(sed '/^$/d' Gasoline)
echo $val3 >> ScriptResult
printf " -------------- Sorted Text ---------------" >> ScriptResult
val4=$(cat Gasoline | sort | uniq -c)
echo $val4 > file
echo $val4 >> ScriptResult
printf " -------------- Most used Words ---------------" >> ScriptResult
sed -e 's/s/ /g' < file | sort | uniq -c | sort -nr | head -10 >> ScriptResult
printf " -------------- Removed Duplicate Words ---------------" >> ScriptResult
sed -r ':a; s/([[:alnum:]]+)(.*)//g; ta; s/(,)+/, /g; s/, *$//' file >> ScriptResult
printf " -------------- Removed punctuation and Converted into lowercase ---------------" >> ScriptResult
val1=$(echo $val4 | tr -d '[:punct:]' | tr '[:upper:]' '[:lower:]')
echo $val1 >> ScriptResult
printf " --------------each word on a single line ---------------" >> ScriptResult
echo $val1 | tr ' ' ' ' >> ScriptResult
#############################################################################################################
# Basic Details : :a define a label a.
# s/([[:alnum:]]+)(.*)//g :
# This looks for a duplicated word consisting of alphanumeric characters and removes the second
# occurrence.
#
# ta : If the last substitution command resulted in a change, this jumps back to label a
# to try again.In this way, the code keeps looking for duplicates until none remain.
#
# s/(, )+/, /g; s/, *$// : These two substitution commands clean up any left over comma-space
# combinations.
#############################################################################################################
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.