Re: Beginner Question (related to homework)



prelim_questions@xxxxxxxxx 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.

Måns Rullgård <mans@xxxxxxxxx> writes:
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---

This however will give a false positive on lines like "." or "42..42"
and false negative on "+42". I'd use case:

#v+
c=0
while read line; do
case "$line" in
(*.*.*|*.|*[!0-9.]*) ;;
esac
done <datafile
echo $c
#v-

This code is intentionally broken as to make OT think, understand and
fix the code before giving it as an answer to the professor. It also
gives false negative on "+42" but it's trivially fixed (hint: use
${foo#...} construct to remove plus or minus sign from the beginning of
variable). This won't consider "4.2e+10" as a numeric.

--
Best regards, _ _
.o. | Liege of Serenly Enlightened Majesty of o' \,=./ `o
..o | Computer Science, Michal "mina86" Nazarewicz (o o)
ooo +--<mina86*tlen.pl>--<jid:mina86*jabber.org>--ooO--(_)--Ooo--
.



Relevant Pages

  • Re: ssh and chroot
    ... ++ echo /bin/mkdir ... ++ awk '' ... ++ grep '^/' ...
    (comp.security.ssh)
  • Re: script doesnt work when run as cron job
    ... ++ echo ';' This is the priv file. ... ++ awk '' ... grep an1 /etc/mtab ...
    (comp.unix.shell)
  • Re: Ignoring trap command for a grep function
    ... > We have a trap statement for every non zero returning command/function. ... when the grep command does not match any lines in the input ... You could use awk instead of grep so you can control the exit status. ... $ echo $? ...
    (comp.unix.programmer)
  • Re: Ignoring trap command for a grep function
    ... > We have a trap statement for every non zero returning command/function. ... when the grep command does not match any lines in the input ... You could use awk instead of grep so you can control the exit status. ... $ echo $? ...
    (comp.unix.shell)
  • Re: Ignoring trap command for a grep function
    ... > We have a trap statement for every non zero returning command/function. ... when the grep command does not match any lines in the input ... You could use awk instead of grep so you can control the exit status. ... $ echo $? ...
    (comp.unix.sco.misc)