LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Question 2 (https://www.linuxquestions.org/questions/linux-newbie-8/question-2-a-4175573159/)

kernkraftx 02-24-2016 08:10 PM

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 {

}

John VV 02-24-2016 08:17 PM

what examples are used in your textbook?

kernkraftx 02-24-2016 08:32 PM

non lol, its the TEACHA!!!!! MAD TEACHA!!! muahahahah, now what...

suicidaleggroll 02-24-2016 09:01 PM

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

berndbausch 02-25-2016 12:16 AM

Quote:

Originally Posted by kernkraftx (Post 5505718)
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.

grail 02-25-2016 01:19 AM

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

pan64 02-25-2016 02:52 AM

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

BW-userx 02-25-2016 08:18 AM

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.

hydrurga 02-25-2016 10:45 AM

Quote:

Originally Posted by BW-userx (Post 5505949)
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". ;-)

kernkraftx 02-25-2016 01:33 PM

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

BW-userx 02-25-2016 01:38 PM

Quote:

Originally Posted by kernkraftx (Post 5506086)
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.

BW-userx 02-25-2016 01:40 PM

Quote:

Originally Posted by kernkraftx (Post 5506086)
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.

BW-userx 02-25-2016 01:53 PM

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

suicidaleggroll 02-25-2016 01:53 PM

Quote:

Originally Posted by kernkraftx (Post 5506086)
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.

grail 02-25-2016 02:13 PM

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


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