LinuxQuestions.org
Visit Jeremy's Blog.
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 02-24-2016, 08:10 PM   #1
kernkraftx
LQ Newbie
 
Registered: Feb 2016
Location: Canada
Distribution: Suse Mint
Posts: 19

Rep: Reputation: Disabled
Question Question 2


So here is my home work :

with bash or awk, but I want to do everything in Awk
a) Read a number
b) Check if this number is digital
c) Finish reading the numbers
d) Calculate the sum
e) Calculate the average (bring the case of division by zero)

My goal is with the awk to formulate first to check if the file has digits and if the file does have digit and letters, will to exclude the letters on the line and finish reading and do the rest, this is what I have so far, but does not seem to work when I do the awk -f theawkfile.awk numbers.awk

so im trying to verify the digits




BEGIN
{

var=$1


}

{ if ( printf "$var" | egrep -q '^\-[0-9]*\.?[0-9]+$' ) ; then
printf "$var is a number"
else
printf "$var is not a number"



}


END {

}
 
Old 02-24-2016, 08:17 PM   #2
John VV
LQ Muse
 
Registered: Aug 2005
Location: A2 area Mi.
Posts: 17,483

Rep: Reputation: 2610Reputation: 2610Reputation: 2610Reputation: 2610Reputation: 2610Reputation: 2610Reputation: 2610Reputation: 2610Reputation: 2610Reputation: 2610Reputation: 2610
what examples are used in your textbook?
 
Old 02-24-2016, 08:32 PM   #3
kernkraftx
LQ Newbie
 
Registered: Feb 2016
Location: Canada
Distribution: Suse Mint
Posts: 19

Original Poster
Rep: Reputation: Disabled
non lol, its the TEACHA!!!!! MAD TEACHA!!! muahahahah, now what...
 
Old 02-24-2016, 09:01 PM   #4
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2137Reputation: 2137Reputation: 2137Reputation: 2137Reputation: 2137Reputation: 2137Reputation: 2137Reputation: 2137Reputation: 2137Reputation: 2137Reputation: 2137
Please describe exactly what you expect the regex pattern to match, what it is matching, and what it is not matching.

As with any question, you need to supply three things:
1) What you're running
2) What you expect it to return
3) What it is, or is not returning, that does not match #2
 
Old 02-25-2016, 12:16 AM   #5
berndbausch
Senior Member
 
Registered: Nov 2013
Location: Tokyo
Distribution: A few
Posts: 4,321

Rep: Reputation: 1229Reputation: 1229Reputation: 1229Reputation: 1229Reputation: 1229Reputation: 1229Reputation: 1229Reputation: 1229Reputation: 1229
Quote:
Originally Posted by kernkraftx View Post
a) Read a number
b) Check if this number is digital
c) Finish reading the numbers
d) Calculate the sum
e) Calculate the average (bring the case of division by zero)
awk's commands are of the form

Code:
pattern { action }
You can use a pattern that identifies a digital number (whatever is that, by the way? Are there analog and digital numbers?). The action part can then accumulate the sum and also count how many numbers were received.

Use an END pattern to output the sum and calculate the average from the sum and the count.
 
Old 02-25-2016, 01:19 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,780

Rep: Reputation: 3056Reputation: 3056Reputation: 3056Reputation: 3056Reputation: 3056Reputation: 3056Reputation: 3056Reputation: 3056Reputation: 3056Reputation: 3056Reputation: 3056
I am a little confused as to what the BEGIN clause is trying to do? BEGIN is executed prior to reading any files, hence there will be no $1 available.

I also have an issue with the problem description:

a) Read a number - - read the number from where?? user input or a file?

b) Check if this number is digital - - others have answered this part

c) Finish reading the numbers - - same as 'a', from where?

d) Calculate the sum - - assuming above has been done, this should be simple

e) Calculate the average (bring the case of division by zero) - - as the average would be based on the number of entries read / entered, if no entries are read then simply say so ... divide by zero should be impossible
 
Old 02-25-2016, 02:52 AM   #7
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 13,899

Rep: Reputation: 4489Reputation: 4489Reputation: 4489Reputation: 4489Reputation: 4489Reputation: 4489Reputation: 4489Reputation: 4489Reputation: 4489Reputation: 4489Reputation: 4489
I think (as it was already written: we do not really know what do you want, but probably) you can find a solution already using google. Similar problems were already solved several times. If you have a script to speak about please post it between code tags: [code]here comes your script[/code] just to keep original formatting.

And finally please do not say your teacher is mad, probably he/she found this post already....
 
Old 02-25-2016, 08:18 AM   #8
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware FreeBSD Win10 MX
Posts: 9,859

Rep: Reputation: 2120Reputation: 2120Reputation: 2120Reputation: 2120Reputation: 2120Reputation: 2120Reputation: 2120Reputation: 2120Reputation: 2120Reputation: 2120Reputation: 2120
Did you even finish your first homemwork assignment that @michaelk and I tried to help you with?

What???

Now you're having "us" do your other homework assignment when you have already deminstrated falure to pay attention to detail from the other post?

Where are the results of your first homework assignment? Post them please in your orginal post for said homework.

Last edited by BW-userx; 02-25-2016 at 08:23 AM.
 
Old 02-25-2016, 10:45 AM   #9
hydrurga
LQ Guru
 
Registered: Nov 2008
Location: Pictland
Distribution: Linux Mint 19.3 MATE
Posts: 8,040
Blog Entries: 5

Rep: Reputation: 2897Reputation: 2897Reputation: 2897Reputation: 2897Reputation: 2897Reputation: 2897Reputation: 2897Reputation: 2897Reputation: 2897Reputation: 2897Reputation: 2897
Quote:
Originally Posted by BW-userx View Post
Did you even finish your first homemwork assignment that @michaelk and I tried to help you with?

What???

Now you're having "us" do your other homework assignment when you have already deminstrated falure to pay attention to detail from the other post?

Where are the results of your first homework assignment? Post them please in your orginal post for said homework.
I'm looking forward to the thread "Question 854". ;-)
 
Old 02-25-2016, 01:33 PM   #10
kernkraftx
LQ Newbie
 
Registered: Feb 2016
Location: Canada
Distribution: Suse Mint
Posts: 19

Original Poster
Rep: Reputation: Disabled
I do not come from an IT background, so please be patient and nice with me, you have no idea, how hard it is. I am here because It's not always understndable waht the teacher explains and I don't blame others for not understanding, here for the other thread, now lets get back to number 5a with sum and read integers. May you still be blessed by jesus, BW-userx.


#!/bin/bash


while [ sortie="true" ]
do

echo -n "Entrez l'heure sous forme hh:mm ou fin terminer :"

read temps

heure= echo "$temps" |cut -d: f1
minute=echo "$temps" |cut -d: f2

if [ $heure = "fin" ]

then
let sortie = "false"

fi

if [[ "$heure" -ge 0 ]] && [[ "$heure" -le 23 ]] && [[ "$minute" -ge 0 ]] && [[ "$minute" -le 59 ]]
then
if [[ "$heure" -ge 6 ]] && [[ "$heure" -le 18 ]]
then
echo "Bonjour"

else

echo "Bonsoir"
fi


fi

sortie = "false"
done
 
Old 02-25-2016, 01:38 PM   #11
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware FreeBSD Win10 MX
Posts: 9,859

Rep: Reputation: 2120Reputation: 2120Reputation: 2120Reputation: 2120Reputation: 2120Reputation: 2120Reputation: 2120Reputation: 2120Reputation: 2120Reputation: 2120Reputation: 2120
Quote:
Originally Posted by kernkraftx View Post
I do not come from an IT background, so please be patient and nice with me, you have no idea, how hard it is.
everybody did not come from a IT background when they first started learning about IT.
 
Old 02-25-2016, 01:40 PM   #12
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware FreeBSD Win10 MX
Posts: 9,859

Rep: Reputation: 2120Reputation: 2120Reputation: 2120Reputation: 2120Reputation: 2120Reputation: 2120Reputation: 2120Reputation: 2120Reputation: 2120Reputation: 2120Reputation: 2120
Quote:
Originally Posted by kernkraftx View Post
I do not come from an IT background, so please be patient and nice with me, you have no idea, how hard it is.
everybody did not come from a IT background when they first started learning about IT.

"May you still be blessed by jesus, BW-userx."

That is actaully a cowards insult. I spoke the truth, and that's Love.

Last edited by BW-userx; 02-25-2016 at 01:42 PM.
 
Old 02-25-2016, 01:53 PM   #13
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware FreeBSD Win10 MX
Posts: 9,859

Rep: Reputation: 2120Reputation: 2120Reputation: 2120Reputation: 2120Reputation: 2120Reputation: 2120Reputation: 2120Reputation: 2120Reputation: 2120Reputation: 2120Reputation: 2120
not wanting to drag this through the mud, but, to make my point.

Just as I stated you do not pay attention to details. Now, compaire what code you posted in here and see you still did not even do what we told you needed to be done. you have not even fixed what little bit that needs to be fixed before you can move on to the next bit of code and get that working also.
Quote:
Improper if statments

use echo to see what's going on in your code

ie:

Code:
read temps

echo "temps: $temps"

heure= "$temps" |cut -d: f1
minute= "$temps" |cut -d: f2

echo "Heure: $heure"
echo "minute: $minute"
review @michaelk hints

Code:
Your reading in two variables, hour and minute but then you want to
use the cut command to split time. Most commands require the - (hyphen)
for specifying options. The correct syntax for the cut command would be

cut -d ":" -f1
and here is a hint for cut:
echo "12:59" | cut -d ":" -f1
you have not even paid attention to what @pan64 told you in this post about this.

If you have a script to speak about please post it between code tags: [code]here comes your script[/code] just to keep original formatting.

you need to get your head striaght and either take this seriously or change majors, or your field of study, which ever applies in this instance, and stop waising other peoples time.

playing the sympathy card will not work with me

Last edited by BW-userx; 02-25-2016 at 02:02 PM.
 
Old 02-25-2016, 01:53 PM   #14
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2137Reputation: 2137Reputation: 2137Reputation: 2137Reputation: 2137Reputation: 2137Reputation: 2137Reputation: 2137Reputation: 2137Reputation: 2137Reputation: 2137
Quote:
Originally Posted by kernkraftx View Post
I do not come from an IT background, so please be patient and nice with me, you have no idea, how hard it is. I am here because It's not always understndable waht the teacher explains and I don't blame others for not understanding, here for the other thread, now lets get back to number 5a with sum and read integers. May you still be blessed by jesus, BW-userx.


#!/bin/bash


while [ sortie="true" ]
do

echo -n "Entrez l'heure sous forme hh:mm ou fin terminer :"

read temps

heure= echo "$temps" |cut -d: f1
minute=echo "$temps" |cut -d: f2

if [ $heure = "fin" ]

then
let sortie = "false"

fi

if [[ "$heure" -ge 0 ]] && [[ "$heure" -le 23 ]] && [[ "$minute" -ge 0 ]] && [[ "$minute" -le 59 ]]
then
if [[ "$heure" -ge 6 ]] && [[ "$heure" -le 18 ]]
then
echo "Bonjour"

else

echo "Bonsoir"
fi


fi

sortie = "false"
done
I recommend looking up a beginner's bash scripting guide. You have a lot of fundamental problems and inconsistencies with your syntax in this small block of code, such as:
1) Variable assignments must not have any spaces around the equal sign
2) Tests, such as 'if [[ "$var1 = "blah" ]]', MUST have spaces around the equal sign
3) Capturing the output of a command is done with $() or ``, you can't just say "var1=program", or it will set the variable "var1" to the string "program", rather than running the program and saving the output
4) Variable assignment is not done with "let"

You should also be posting your code in [code][/code] brackets to preserve formatting, such as indentation and white space.
 
Old 02-25-2016, 02:13 PM   #15
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,780

Rep: Reputation: 3056Reputation: 3056Reputation: 3056Reputation: 3056Reputation: 3056Reputation: 3056Reputation: 3056Reputation: 3056Reputation: 3056Reputation: 3056Reputation: 3056
Well my questions is ... how did this change from awk to bash all of a sudden? ... Talk about not paying attention
 
  


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
[SOLVED] GPL question (Version 2, June 1991) - physical media availability question LicenseQuestions Linux - Newbie 1 12-01-2012 06:34 PM
basic html question - download link to files on my webpage question Davno Linux - Server 5 12-25-2009 07:24 AM
Question, Apples Contribution to Open Source + MacOs file structure question Higgy3k Other *NIX 5 07-25-2005 04:23 AM
Not your regular GRUB question - just a short question for a fried MBR!! ziphem Linux - General 3 01-31-2005 01:51 PM
login prompt question & kde scheme question JustinCoyan Slackware 2 06-09-2004 02:02 PM

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

All times are GMT -5. The time now is 02:13 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration