Beginner Question (related to homework)
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Programming > Beginner Question (related to homework)




Pages (2): [1] 2 »   Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    Beginner Question (related to homework)  
prelim_questions@yahoo.com


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
09-25-07 12:23 AM

I need to perform a complicated (for me) operation at the command line
using a series of pipes, but not using grep, sed, or awk.

At one point I may have a list as follows:

1.2
3.45
6.7
a
bc


Is there an easy way to count only the numeric lines?  I do not know
in advance how many lines are numbers and how many are strictly
alphabetic.  There are no mixed lines.

Thanks in advance.






[ Post a follow-up to this message ]



    Re: Beginner Question (related to homework)  
prelim_questions@yahoo.com


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
09-25-07 12:23 AM

Ok.  I think for my purpose I can use:    >>    ... |  sort  | xargs
-E   firststring   echo |  wc -w

More generally, I might prefer grep [0-9] datafile, but I can't use
that.

But in my first example, what if I didn't know a priori the first
character string?




On Sep 24, 5:34 pm, prelim_questi...@yahoo.com wrote:
> I need to perform a complicated (for me) operation at the command line
> using a series of pipes, but not using grep, sed, or awk.
>
> At one point I may have a list as follows:
>
> 1.2
> 3.45
> 6.7
> a
> bc
>
> Is there an easy way to count only the numeric lines?  I do not know
> in advance how many lines are numbers and how many are strictly
> alphabetic.  There are no mixed lines.
>
> Thanks in advance.







[ Post a follow-up to this message ]



    Re: Beginner Question (related to homework)  
Chris McDonald


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
09-25-07 12:23 AM

prelim_questions@yahoo.com writes:

>Ok.  I think for my purpose I can use:    >>    ... |  sort  | xargs
>-E   firststring   echo |  wc -w

>More generally, I might prefer grep [0-9] datafile, but I can't use
>that.

>But in my first example, what if I didn't know a priori the first
>character string?


A pattern for a switch statement in the shell?


(personally I find these contrived homework exercises, where you must
perform a task but may not use the generally agreed upon best tools or
approaches, to be rather, well, contrived.  They are generally only set
to stroke the ego of the professor who has 'invented' an alternative
approach.  Challenge them - ask *why* you can't use the best approaches.)

 ________________________________________
____________________________________
__
Dr Chris McDonald                          E: chris@csse.uwa.edu.au
Computer Science & Software Engineering    W: [url]http://www.csse.uwa.edu.au/~chris[/u
rl]
The university of Western Australia, M002  T: +618 6488 2533
Crawley, Western Australia, 6009           F: +618 6488 1089





[ Post a follow-up to this message ]



    Re: Beginner Question (related to homework)  
Måns Rullgård


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
09-25-07 12:23 AM

prelim_questions@yahoo.com writes:

> I need to perform a complicated (for me) operation at the command line
> using a series of pipes, but not using grep, sed, or awk.
>
> At one point I may have a list as follows:
>
> 1.2
> 3.45
> 6.7
> a
> bc
>
> Is there an easy way to count only the numeric lines?  I do not know
> in advance how many lines are numbers and how many are strictly
> alphabetic.  There are no mixed lines.

This task is so stupid that I'll answer it even though it's homework.

---8<---
c=0
while read line; do
test "${line#*[^0-9.]}" = "$line" && c=$(expr $c + 1)
done < datafile
echo $c
--->8---

There, I bet that wasn't what the instructor had in mind.  Counting
alphabetic lines is left as an exercise for the reader, as is checking
that there is at most one decimal point per line.

--
Måns Rullgård
mans@mansr.com





[ Post a follow-up to this message ]



    Re: Beginner Question (related to homework)  
Soeren Sandmann


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
09-25-07 06:29 AM

prelim_questions@yahoo.com writes:

> Is there an easy way to count only the numeric lines?  I do not know
> in advance how many lines are numbers and how many are strictly
> alphabetic.  There are no mixed lines.

This:

... | ((xargs printf "%f" > /dev/null) 2> /dev/null) && echo fish) | wc -l

should work, but maybe it's cheating since it essentially uses printf
as a poor man's grep.





[ Post a follow-up to this message ]



    Re: Beginner Question (related to homework)  
Barry Margolin


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
09-25-07 06:29 AM

In article <fd9dfr$bv1$1@enyo.uwa.edu.au>,
Chris McDonald <chris@csse.uwa.edu.au> wrote:

> prelim_questions@yahoo.com writes:
> 
> 
> 
>
>
> A pattern for a switch statement in the shell?
>
>
> (personally I find these contrived homework exercises, where you must
>  perform a task but may not use the generally agreed upon best tools or
>  approaches, to be rather, well, contrived.  They are generally only set
>  to stroke the ego of the professor who has 'invented' an alternative
>  approach.  Challenge them - ask *why* you can't use the best approaches.)

Because they're trying to teach you particular features of the shell,
such as while loops and case statements, or specific other tools.

Imagine you're taking a swimming class, and the teacher tells you to
practice doing laps doing the backstroke.  Would you ask why you can't
do a crawl, since it's a much more efficient way to get across the pool?

--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***





[ Post a follow-up to this message ]



    Re: Beginner Question (related to homework)  
Måns Rullgård


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
09-25-07 12:31 PM

Barry Margolin <barmar@alum.mit.edu> writes:

> In article <fd9dfr$bv1$1@enyo.uwa.edu.au>,
>  Chris McDonald <chris@csse.uwa.edu.au> wrote:
> 
>
> Because they're trying to teach you particular features of the shell,
> such as while loops and case statements, or specific other tools.

If that's the intent, the examples should be ones where using those
constructs make sense.  Otherwise the student will have learned
nothing useful at all.

--
Måns Rullgård
mans@mansr.com





[ Post a follow-up to this message ]



    Re: Beginner Question (related to homework)  
David Schwartz


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
09-26-07 12:31 PM

On Sep 24, 11:14 pm, Barry Margolin <bar...@alum.mit.edu> wrote:

> Because they're trying to teach you particular features of the shell,
> such as while loops and case statements, or specific other tools.

Would you teach someone how to use a hammer by telling him to use it
to put in a screw?

> Imagine you're taking a swimming class, and the teacher tells you to
> practice doing laps doing the backstroke.  Would you ask why you can't
> do a crawl, since it's a much more efficient way to get across the pool?

Well, in real life, there are races where you must do the backstroke.
Practicing doing the backstroke is helpful for this real life
activity. If there were some corresponding real life situation this
exercise prepared you for, it would be perfectly reasonable.

DS






[ Post a follow-up to this message ]



    Re: Beginner Question (related to homework)  
Rainer Weikusat


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
09-26-07 12:31 PM

David Schwartz <davids@webmaster.com> writes:
> On Sep 24, 11:14 pm, Barry Margolin <bar...@alum.mit.edu> wrote: 
>
> Would you teach someone how to use a hammer by telling him to use it
> to put in a screw?

The problem can be solved using general programming constructs and it
can be solved by using code written by someone else which in turn uses
general programming constructs. So, a better question would be 'Could
you teach someone how to program an algorithm by having him memorize
quirks and idiosyncrasies of specific (GNU-)programs happening to
implement it?'.





[ Post a follow-up to this message ]



    Re: Beginner Question (related to homework)  
Stephane CHAZELAS


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
09-27-07 12:20 AM

2007-09-24, 14:34(-07), prelim_questions@yahoo.com:
> I need to perform a complicated (for me) operation at the command line
> using a series of pipes, but not using grep, sed, or awk.
>
> At one point I may have a list as follows:
>
> 1.2
> 3.45
> 6.7
> a
> bc
>
>
> Is there an easy way to count only the numeric lines?  I do not know
> in advance how many lines are numbers and how many are strictly
> alphabetic.  There are no mixed lines.
[...]

If it has to be convoluted and not obvious and not legible, you
could do things like:

< list tr -cs 0-9. '[\n*]' | wc -l

of course, that doesn't work if there are lines containing both
alphabetics and numbers, or lines with digits and several dots...

--
Stéphane





[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 01:37 AM.      Post New Thread    Post A Reply      
Pages (2): [1] 2 »   Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register