Re: Beginner Question (related to homework)
- From: Michal Nazarewicz <mina86@xxxxxxx>
- Date: Fri, 28 Sep 2007 10:29:20 +0200
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--
.
- References:
- Beginner Question (related to homework)
- From: prelim_questions
- Re: Beginner Question (related to homework)
- From: Måns Rullgård
- Beginner Question (related to homework)
- Prev by Date: Re: declaring a function as static..
- Next by Date: Re: declaring a function as static..
- Previous by thread: Re: Beginner Question (related to homework)
- Next by thread: Re: Beginner Question (related to homework)
- Index(es):
Relevant Pages
|