[SOLVED] Seeking advice with shell script (homework)
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I have been working on this in the background, below is what I have compiled thus far, any thoughts?
Code:
#!/bin/bash
# Bash TestScript
#a. Remove punctuation; #b. Make all characters lowercase
tr -d '[:punct:]' < Gasoline | tr '[:upper:]' '[:lower:]' tee ScriptResults
#c. Put each word on a line by itself
tr ' ' '\n' < gasoline
#d. Remove blank lines; #e. Sort the text to pull all lines containing the same word on adjacent lines; #f. Remove duplicate words from the text
sed '/^$/d' | sort | uniq -c
#g. List most used words in the file first
cat ScriptResults | tr ' ' '\012' | tr '[:upper:]' '[:lower:]' | tr -d '[:punct:]' | grep -v '[^a-z]' | sort | uniq -c | sort -rn | head
exit 0
I think I am close, the command sequence below (I am assuming as I have not tried it in pyrite yet)will complete steps A - E...A second pair of eyes would be helpful with my assumption.
I took it one tr command at a time, then rearranged them in a different order of operation.
first I remove the punctuation putting a space in place of it to get that 20word to 20 word, so I an put each one on a new line each, then change the caps to lower case. then put it into another file, then send it to sed to print out on the screen, I have no idea what that sed command actually does, I don't play with sed, nor tr ... I only learn what I have to as I go to get my scripts to work to do what I actually need them to do. more real world applications in my experience.
don't you still have to get a count of like words per your original post?
I have read the specifications again. If you follow them exactly in order (as I am guessing your teacher has as she wrote them), it is possible to deliver the output required.
Below is the list of command in order without the what-to-do:
Code:
tr | tr | tr | sed | sort | uniq | sort
Using the above order I was able to generate the following output:
Code:
2 a
2 prices
1 20
1 an
1 continuing
1 gallon
1 gas
1 half
1 in
1 mostly
1 of
1 only
1 past
1 penny
1 rose
1 steady
1 the
1 trend
1 two
1 unusual
1 week
1 weeks
Thank you all especially Grail and BW-Userx your help on this has been great. Below is the code string (script) I used which gave the results needed. At first I used the tee ScriptResults and it gave an error so I removed the tee with the > symbol and it worked without errors. This has been a true learning experience, I was over complicating the whole thing, a lack of understanding in this OS for sure... Again thank you.
FYI the second < Gasoline (tr ' ' '\n' < Gasoline) is not necessary since your piping the output from the previous command. Would not want you to lose points...
Gas Prices Rose Only half a penny a, gallon in the Past two weeks, continuing an unusual 20-week trend of mostly steady prices
results:
Quote:
1 weeks,
1 unusual
1 two
1 trend
1 the
1 steady
1 prices
1 penny
1 of
1 mostly
1 in
1 half
1 gallon
1 continuing
1 an
1 a,
1 a
1 Rose
1 Prices
1 Past
1 Only
1 Gas
1 20-week
command:
Code:
# punctuation are still there the , and - ?
tr -d '[:punct:]' < gas | tr '[:upper:]' '[:lower:]' | tr ' ' '\n' < gas | sed '/^$/d' | sort | uniq -c | sort -rn > ScriptResults
#Capitalization is still there too.
try it without the exit 0 that is something that should not even be needed in a script like this.
think about the order in which you preform each step of the complete operation.
preforms one operation then pipes the results into the next operation in the format that the last operation performed on the data.
# punctuation are still there the , and - ?
tr -d '[:punct:]' < gas | tr '[:upper:]' '[:lower:]' | tr ' ' '\n' < gas | sed '/^$/d' | sort | uniq -c | sort -rn > ScriptResults
#Capitalization is still there too.
like the other said only need to redirect the file into it once. No I am not even giving up that answer.
try it without the exit 0 that is something that should not even be needed in a script like this.
think about the order in which you preform each step of the complete operation.
Did the recommended changes and you were correct the exit 0 and the second > Gasoline part was not needed... Again thank you.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.