LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 04-07-2016, 11:06 AM   #16
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242

Quote:
Originally Posted by sobey View Post
Code:
 -bash-3.2$ sed -ri .s/(.* )1/1/g. Gasoline
-bash: syntax error near unexpected token `('
Code:
sed -ri 's/(.* )1/1/g'
quotes so that the system sees it as one complete argument.
 
Old 04-07-2016, 11:16 AM   #17
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,691

Rep: Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894
That will fix the syntax error but will not give the expected results.

Code:
 sed -ri 's/(.* )\1/\1/g' gasoline

Last edited by michaelk; 04-07-2016 at 11:23 AM.
 
Old 04-07-2016, 11:20 AM   #18
sobey
LQ Newbie
 
Registered: Apr 2016
Posts: 16

Original Poster
Rep: Reputation: Disabled
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
 
Old 04-07-2016, 12:07 PM   #19
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
it doesn't exit it hangs in the terminal on mine NE ways.
 
Old 04-07-2016, 01:05 PM   #20
sobey
LQ Newbie
 
Registered: Apr 2016
Posts: 16

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by BW-userx View Post
it doesn't exit it hangs in the terminal on mine NE ways.
To prevent it from hanging would exit 0 work or would I need to input something else?
 
Old 04-07-2016, 01:26 PM   #21
sobey
LQ Newbie
 
Registered: Apr 2016
Posts: 16

Original Poster
Rep: Reputation: Disabled
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.

Code:
tr -d '[:punct:]' < Gasoline | tr '[:upper:]' '[:lower:]' | tr ' ' '\n' < gasoline | sed '/^$/d' | sort | uniq -c tee ScriptResults
 
Old 04-07-2016, 01:27 PM   #22
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,691

Rep: Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894
gasoline should only appear once as input.

Last edited by michaelk; 04-07-2016 at 01:29 PM.
 
Old 04-07-2016, 02:23 PM   #23
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
it is still hanging on me. it is like this one almost works. So is that cheeting????

reference you're work and this page

Code:
#!/bin/bash
cat gas | tr -ds [:upper:]  [:lower:] | tr ' ' '\n' > ScriptResults | sed '/^$/d' ScriptResults
mod: scrap my idea ..

Last edited by BW-userx; 04-07-2016 at 02:35 PM.
 
1 members found this post helpful.
Old 04-07-2016, 02:52 PM   #24
sobey
LQ Newbie
 
Registered: Apr 2016
Posts: 16

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by BW-userx View Post
it is still hanging on me. it is like this one almost works. So is that cheeting????

reference you're work and this page

Code:
#!/bin/bash
cat gas | tr -ds [:upper:]  [:lower:] | tr ' ' '\n' > ScriptResults | sed '/^$/d' ScriptResults
mod: scrap my idea ..
I am going to try the script below and name it TestScript, would running it like this still hang?

Code:
#!/bin/bash
# Bash TestScript

tr -d '[:punct:]' < Gasoline | tr '[:upper:]' '[:lower:]' | tr ' ' '\n' < gasoline | sed '/^$/d' | sort | uniq -c | sort -rn tee ScriptResults

exit 0
 
Old 04-07-2016, 02:58 PM   #25
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by sobey View Post
I am going to try the script below and name it TestScript, would running it like this still hang?

Code:
#!/bin/bash
# Bash TestScript

tr -d '[:punct:]' < Gasoline | tr '[:upper:]' '[:lower:]' | tr ' ' '\n' < gasoline | sed '/^$/d' | sort | uniq -c | sort -rn tee ScriptResults

exit 0
try just this without the exit 0 tell me what you think
this seperates that 20week to make it,
20
week

it was really bugging me so I worked it out for ya.
Code:
#!/bin/bash
cat gasoline | tr -s ", -" " " |  tr ' ' '\n'  | tr '[:upper:]' '[:lower:]' > ScriptResults | sed '/^$/d' ScriptResults ScriptResults
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?

http://www.linuxquestions.org/questi...rk-4175485733/

Last edited by BW-userx; 04-07-2016 at 03:42 PM.
 
1 members found this post helpful.
Old 04-07-2016, 04:17 PM   #26
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
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
 
1 members found this post helpful.
Old 04-07-2016, 07:49 PM   #27
sobey
LQ Newbie
 
Registered: Apr 2016
Posts: 16

Original Poster
Rep: Reputation: Disabled
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.

Code:
#!/bin/bash
# Bash TestScript

tr -d '[:punct:]' < Gasoline | tr '[:upper:]' '[:lower:]' | tr ' ' '\n' < Gasoline | sed '/^$/d' | sort | uniq -c | sort -rn > ScriptResults

exit 0
 
Old 04-07-2016, 07:58 PM   #28
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,691

Rep: Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894
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...
 
1 members found this post helpful.
Old 04-07-2016, 08:04 PM   #29
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
data:
Quote:
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.

is it just me ? I don't know.

Last edited by BW-userx; 04-07-2016 at 08:26 PM.
 
1 members found this post helpful.
Old 04-07-2016, 08:14 PM   #30
sobey
LQ Newbie
 
Registered: Apr 2016
Posts: 16

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by BW-userx View Post
data:

results:



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


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Seeking advice with shell script (homework) mmmm13 Linux - Newbie 10 11-25-2013 11:15 AM
I need advice to create a simple script (homework) anas_lko Linux - Newbie 12 02-01-2012 10:59 AM
Shell Script (Yes it is a homework question) smturner1 Linux - Newbie 11 11-02-2009 10:11 PM
Seeking advice on bash script satimis Programming 6 10-11-2004 11:01 AM
Seeking advice on script satimis Programming 1 10-05-2004 03:02 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:19 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration