Linux 1 Create and save a new text file called Gasoline that

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.

Solution

#################
# 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/\\b([[:alnum:]]+)\\b(.*)\\b\\1\\b/\\1\\2/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/\\b([[:alnum:]]+)\\b(.*)\\b\\1\\b/\\1\\2/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.
#############################################################################################################

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
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

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site