LinuxQuestions.org
Help answer threads with 0 replies.
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 11-24-2013, 01:42 PM   #1
mmmm13
LQ Newbie
 
Registered: Nov 2013
Posts: 9

Rep: Reputation: Disabled
Question Seeking advice with shell script (homework)


I just started working with Linux/UBUNTU scripts and i'm having a really hard time. I believe this is basic, but i'm just not good with this type of scripts. If anyone can help I would really appreciate it.

This is what I have so far, i'm probably going in the wrong direction with this, but I've been trying.

tr -d [unct:] < Gasoline;
tr [A-Z [a-z} < Gasoline;
tr ' ' '\n' < Gasoline;
tr -d [:space:] < Gasoline;

Like I said i'm possibly going in the wrong direction with it, but i'm just stuck.

Create a text file with this information. (completed)

Gas prices rose only half a penny a gallon in the past two weeks, continuing an unusual 20-week trend of mostly steady prices.

3. 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 pull all lines containing the same word on adjacent lines

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 ScriptResults

Last edited by mmmm13; 11-24-2013 at 01:56 PM.
 
Old 11-24-2013, 05:03 PM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 17,925

Rep: Reputation: 3690Reputation: 3690Reputation: 3690Reputation: 3690Reputation: 3690Reputation: 3690Reputation: 3690Reputation: 3690Reputation: 3690Reputation: 3690Reputation: 3690
Quote:
Originally Posted by mmmm13 View Post
I just started working with Linux/UBUNTU scripts and i'm having a really hard time. I believe this is basic, but i'm just not good with this type of scripts. If anyone can help I would really appreciate it. This is what I have so far, i'm probably going in the wrong direction with this, but I've been trying.

tr -d [unct:] < Gasoline;
tr [A-Z [a-z} < Gasoline;
tr ' ' '\n' < Gasoline;
tr -d [:space:] < Gasoline;

Like I said i'm possibly going in the wrong direction with it, but i'm just stuck.

Create a text file with this information. (completed)
Gas prices rose only half a penny a gallon in the past two weeks, continuing an unusual 20-week trend of mostly steady prices.
3. 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 pull all lines containing the same word on adjacent lines
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 ScriptResults
We will be glad to help...post the input text file, and what you've done so far. There has to be more than just a few 'tr' commands that you've done...so post the script and what error(s)/bad output you're getting. Which part are you stuck on??
 
1 members found this post helpful.
Old 11-24-2013, 05:33 PM   #3
mmmm13
LQ Newbie
 
Registered: Nov 2013
Posts: 9

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by TB0ne View Post
We will be glad to help...post the input text file, and what you've done so far. There has to be more than just a few 'tr' commands that you've done...so post the script and what error(s)/bad output you're getting. Which part are you stuck on??
I just don't know where to start to convert it to a script, so far what I posted is all I have and i'm just stuck there for about a day now, read the chapter twice, doesn't say crap about using tr in script and that's where I am at. Still trying to figure it out though, been looking at several forums now.
 
Old 11-24-2013, 08:07 PM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,247

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
Well as it is homework we will ask you some questions which hopefully lead in the correct direction

1. Not to do with your homework but when posting here, please use [code][/code] tags around your code and data so it is easier to read and keeps formatting

2. What is your understanding of what a script is?

3. Which shell are you working with? (Been a while since I used Ubuntu but the default used to be dash at the command line, hence a bash script would have slightly different structure)

4. Is 'tr' the only tool you have currently learnt? (There are several that could be applicable and you could also complete the entire task in the shell (potentially))

5. Was the original file created under Ubuntu or Windows? (This is something you really need to pay attention to)


I am sure there is more but this should help us to help you and hopefully lead you in a general direction
 
1 members found this post helpful.
Old 11-24-2013, 08:29 PM   #5
mmmm13
LQ Newbie
 
Registered: Nov 2013
Posts: 9

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by grail View Post
Well as it is homework we will ask you some questions which hopefully lead in the correct direction :)

1. Not to do with your homework but when posting here, please use [code][/code] tags around your code and data so it is easier to read and keeps formatting :)

2. What is your understanding of what a script is?

3. Which shell are you working with? (Been a while since I used Ubuntu but the default used to be dash at the command line, hence a bash script would have slightly different structure)

4. Is 'tr' the only tool you have currently learnt? (There are several that could be applicable and you could also complete the entire task in the shell (potentially))

5. Was the original file created under Ubuntu or Windows? (This is something you really need to pay attention to)

I am sure there is more but this should help us to help you and hopefully lead you in a general direction :)
(2) My understanding of a script is working through shell and creating programmable code. (which I still don't get the concept)

(3)I am working with Bash shell

(4)tr is not the only thing I have learned, but from what I know tr are the only ones you can use to remove punctuation, lowercase and removing blank lines.

(5)I am using a Linux terminal and creating the script using vi.

What I am trying to get to is to complete the entire task through shell but don't know really where to start.
 
Old 11-24-2013, 11:54 PM   #6
John VV
LQ Muse
 
Registered: Aug 2005
Location: A2 area Mi.
Posts: 16,818

Rep: Reputation: 2408Reputation: 2408Reputation: 2408Reputation: 2408Reputation: 2408Reputation: 2408Reputation: 2408Reputation: 2408Reputation: 2408Reputation: 2408Reputation: 2408
Quote:
)I am using a Linux terminal and creating the script using vi.
VI / vim are GREAT
BUT

if you are new to the linux commands and programs "vi" can be VERY VERY OVERWHELMING

ubuntu defaults to the Gnome desktop
i would use "gedit" to edit a shell script

as you recall they ALL start with a shebang
Code:
#!/bin/sh
from the first post it sounds like you have to t=do many things to the text file

Start with doing ONE AT A TIME !!!

this below WILL NOT change the text in a file to lower case BUT will rename all the files in a folder from UPPERCASE to lowercase ( handy when Windows saves things with all CAP file extensions - a folder full of *.JPG and you want all lowercase )
as an example for ONE THING ONLY
TEXT1.TXT > text1.txt
Code:
#!/bin/sh

for i in *; do mv "$i" "$(echo $i | tr '[A-Z]' '[a-z]')"; done
work out each objective
then merg them together

also have a look as
sed
grep
cat
awk
and bash ( there are built in functions )


and a second look at sed
and search the web for " sed one liners"
like this
http://www.catonmat.net/blog/sed-one...ined-part-one/
http://www.catonmat.net/blog/wp-cont...9/sed1line.txt

http://www.osnews.com/story/21004
http://www.catonmat.net/series/

PS
if your Prof is any good
they already know the main sed one line commands
so DO NOT just copy / past a bunch
--- they WILL KNOW !!!!

Last edited by John VV; 11-25-2013 at 12:04 AM.
 
1 members found this post helpful.
Old 11-25-2013, 12:18 AM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,247

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
Ok ... thanks for the information

About (2), when you write a command on the command line (CLI) it is processed by the shell (for things likes globs, expansions and so on) and then that information passed to the command you are using.
So using one of your examples:
Code:
tr -d [:punct:] < Gasoline;
On the CLI the semi-colon ( at the end is not required unless entering more than one command after each other

The shell will use its own interpretation for anything that is not a command and not quoted, hence entering [unct:] is potentially dangerous unless you are sure the shell will not interpret it

Once interpretation is completed the information is then passed to the command (tr in your case) and it performs its necessary tasks

Note: You do also have redirection here but as you have used it I will assume you understand what it is doing.

Now if you wanted to enter all of the commands from your first post, your choices are:

1. Enter each command on a single line of the CLI

2. Enter all commands on the CLI separated by a semi-colon

3. Place all command in a file and using an interpreter (in your case bash) to execute the commands in the file. This also has multiple options:

a. Simplest case is to put the commands in the file, we will call script.sh, and execute thusly:
Code:
bash script.sh
b. Again place the commands in the file and at the top of the file you identify which interpreter (this should have been explained already in your class) to use: (you will also need to make the file executable)
Code:
./script.sh
(4) What about commands like sed or awk?
 
1 members found this post helpful.
Old 11-25-2013, 01:49 AM   #8
mmmm13
LQ Newbie
 
Registered: Nov 2013
Posts: 9

Original Poster
Rep: Reputation: Disabled
I really appreciate the help guys, comes out that one of my class mates helped me out. It was very simple, just had a little more trouble when I was sorting, but it's all well. Not sure to post the solution as this is a homework assignment. Appreciate the help to the ones that tried to help.

PS I wouldn't try to cheat I just needed some direction

Last edited by mmmm13; 11-25-2013 at 01:53 AM.
 
Old 11-25-2013, 04:24 AM   #9
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,247

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
Glad you found your way. I agree that posting the solution would not perhaps be the best thing to do, but maybe you could outline what helped you to solve where you were stuck.
That way the next poor soul who finds your post can have some ideas on what he/she should look for

Please remember to mark as SOLVED one you have a solution
 
1 members found this post helpful.
Old 11-25-2013, 09:34 AM   #10
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 17,925

Rep: Reputation: 3690Reputation: 3690Reputation: 3690Reputation: 3690Reputation: 3690Reputation: 3690Reputation: 3690Reputation: 3690Reputation: 3690Reputation: 3690Reputation: 3690
Quote:
Originally Posted by mmmm13 View Post
I really appreciate the help guys, comes out that one of my class mates helped me out. It was very simple, just had a little more trouble when I was sorting, but it's all well. Not sure to post the solution as this is a homework assignment. Appreciate the help to the ones that tried to help.

PS I wouldn't try to cheat I just needed some direction
My posting signature has a link to a scripting tutorial...that's a good start. There are also MANY you can find via Google as well, with examples, on how to write shell scripts for Linux.

To put it in Windows-terms, a shell script is like a Windows batch file...nothing but a list of commands you can type in at the command line, with a few extra capabilities (like looping, variables, etc.). If you know the commands to type in, you've essentially got your shell script. Reading the man pages on things like sort and uniq would be good places to start as well.
 
1 members found this post helpful.
Old 11-25-2013, 12:15 PM   #11
mmmm13
LQ Newbie
 
Registered: Nov 2013
Posts: 9

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by grail View Post
Glad you found your way. I agree that posting the solution would not perhaps be the best thing to do, but maybe you could outline what helped you to solve where you were stuck.
That way the next poor soul who finds your post can have some ideas on what he/she should look for

Please remember to mark as SOLVED one you have a solution
It was just a long line of command using piping, it was sort of simple, except the sorting part...

cat input_file | command | command |command | command | > output_file

This is, I believe pretty basic but I was really struggling with it.

Once again thanks again guys.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
I need advice to create a simple script (homework) anas_lko Linux - Newbie 12 02-01-2012 11:59 AM
[SOLVED] Seeking advice for user creation script ramkumarramki Linux - Newbie 5 12-22-2011 07:50 PM
Shell Script (Yes it is a homework question) smturner1 Linux - Newbie 11 11-02-2009 11:11 PM
Seeking advice on bash script satimis Programming 6 10-11-2004 12:01 PM
Seeking advice on script satimis Programming 1 10-05-2004 04:02 PM


All times are GMT -5. The time now is 02:40 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration