Mailinglist Archive: opensuse (2430 mails)
| < Previous | Next > |
Re: [opensuse] BASH - brain teaser, can it be done without a pipe?
- From: Anders Johansson <ajh@xxxxxxxxxx>
- Date: Sat, 19 Apr 2008 12:09:16 +0200
- Message-id: <200804191209.16271.ajh@xxxxxxxxxx>
On Saturday 19 April 2008 11:35:49 David C. Rankin wrote:
One solution would be to do it in another language, like perl or python, that
can do floating point
But in bash, something like this might work
tmax=0; while read a b c d; do tmpnum=${c/\./}; if [ $tmpnum -gt $tmax ]; then
tmax=$tmpnum; fi; done < temps; tmax="${tmax:0:$((${#tmax}-1))}.
${tmax: -1:1}"; echo "Max temp is $tmax"
It assumes of course that there is only one decimal, and that it is always
there.
sed is almost never needed for things like this. bash can do it internally in
almost all cases
Anders
--
To unsubscribe, e-mail: opensuse+unsubscribe@xxxxxxxxxxxx
For additional commands, e-mail: opensuse+help@xxxxxxxxxxxx
The problem was that I had to strip the "." from the temp reading to do
the integer comparison in BASH and then I had to add it back to the output
to get the decimal place back into the variable. (I know I don't need the
decimal to know what the max temp was, but I wanted it there and I though I
could learn something in the process)
One solution would be to do it in another language, like perl or python, that
can do floating point
But in bash, something like this might work
tmax=0; while read a b c d; do tmpnum=${c/\./}; if [ $tmpnum -gt $tmax ]; then
tmax=$tmpnum; fi; done < temps; tmax="${tmax:0:$((${#tmax}-1))}.
${tmax: -1:1}"; echo "Max temp is $tmax"
It assumes of course that there is only one decimal, and that it is always
there.
With all the efficiency discussion on the list about reducing pipes, I
though I would pose this cl and ask if there is a way to have sed parse the
variables $t and $tmax without the echo and | I used above. I tried several
things, but none worked (i.e. sed -e 's/\.//' < echo "$t"). Also, if there
is a simpler way to do what I did, by all means let me know.
sed is almost never needed for things like this. bash can do it internally in
almost all cases
Anders
--
To unsubscribe, e-mail: opensuse+unsubscribe@xxxxxxxxxxxx
For additional commands, e-mail: opensuse+help@xxxxxxxxxxxx
| < Previous | Next > |