|
Home > Archive > Unix Shell > February 2007 > Arithmetic on an array
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Arithmetic on an array
|
|
|
| Say I have a text file, characters 10-16 are numbers, which I'm going to cut
into an array as below (ksh)
array=(`cut -c 10-16 $filename`)
How can I then add up all the numbers in the array and store this as a
variable?
| |
| Bill Marcum 2007-02-24, 1:16 pm |
| On Sat, 24 Feb 2007 12:30:45 GMT, Ben H
<ben@noone.com> wrote:
>
>
> Say I have a text file, characters 10-16 are numbers, which I'm going to cut
> into an array as below (ksh)
>
> array=(`cut -c 10-16 $filename`)
>
> How can I then add up all the numbers in the array and store this as a
> variable?
>
for x in ${array[*]}; do
sum=$((sum+x))
done
--
"Hello," he lied.
-- Don Carpenter, quoting a Hollywood agent
| |
| kruhft 2007-02-24, 1:16 pm |
| Ben H wrote:
> Say I have a text file, characters 10-16 are numbers, which I'm going to cut
> into an array as below (ksh)
>
> array=(`cut -c 10-16 $filename`)
>
> How can I then add up all the numbers in the array and store this as a
> variable?
I'm not sure of the syntax for expanding an array variable in ksh, but
in bash you can use a line like this:
sum=$(expr `echo ${array[*]} | sed 's| | + |g'`)
This converts the array to a series of values and +'s which are used
as arguments to an expr command. An even more modern bash way is:
sum=$((sed 's| | + |g' <<< ${array[*]}`))
Which uses the <<< here string operator and the $(( )) arithmetic
brace expansion.
--
kruhft
| |
| Stephane CHAZELAS 2007-02-24, 1:16 pm |
| 2007-02-24, 12:30(+00), Ben H:
> Say I have a text file, characters 10-16 are numbers, which I'm going to cut
> into an array as below (ksh)
>
> array=(`cut -c 10-16 $filename`)
>
> How can I then add up all the numbers in the array and store this as a
> variable?
Why do you need an array?
awk '{ sum += substr($0, 10, 7) } END { print sum }' < "$filename"
Remember shells are the tools to run commands / interpret
command lines. There's no better suited command than awk to
extract information from a file line by line and do arithmetics
on it.
If that's a programming language you're after please consider
using programming languages (ruby, python, perl...) not a shell.
--
Stéphane
| |
| Chris F.A. Johnson 2007-02-24, 1:16 pm |
| On 2007-02-24, Ben H wrote:
> Say I have a text file, characters 10-16 are numbers, which I'm going to cut
> into an array as below (ksh)
>
> array=(`cut -c 10-16 $filename`)
>
> How can I then add up all the numbers in the array and store this as a
> variable?
You can bypass the array:
total=$(( `cut -c 10-16 $filename | tr '\012' +` 0 ))
--
Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
| |
|
| Thanks, but could you please explain what the below does
tr '\012' +` 0 ))
Also, why is the zero needed at the end?
Thanks.
"Chris F.A. Johnson" <cfajohnson@gmail.com> wrote in message
news:j0b6b4-n7k.ln1@xword.teksavvy.com...
> On 2007-02-24, Ben H wrote:
>
> You can bypass the array:
>
> total=$(( `cut -c 10-16 $filename | tr '\012' +` 0 ))
>
> --
> Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell>
> Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
> ===== My code in this post, if any, assumes the POSIX locale
> ===== and is released under the GNU General Public Licence
| |
| Janis Papanagnou 2007-02-25, 1:17 pm |
| Ben H wrote:
[Please don't top-post]
> Thanks, but could you please explain what the below does
>
> tr '\012' +` 0 ))
That does nothing but produce a syntax error.
Decompose Chris' complete expression...
`cut -c 10-16 $filename | tr '\012' +` 0
cut -c 10-16 $filename
will extract the relevant part from the file to standard output
tr '\012' +
will replace each newline character \012 by a plus character
1
2
3
will result in 1+2+3+
`...`
will expand the text output from the embedded command ..., in
this case to a single line separated by a space.
To be a valid arithmetic expression you must either remove the
trailling + or more simply in this case just add the value 0.
Janis
>
> Also, why is the zero needed at the end?
>
> Thanks.
>
>
> "Chris F.A. Johnson" <cfajohnson@gmail.com> wrote in message
> news:j0b6b4-n7k.ln1@xword.teksavvy.com...
>
>
>
>
|
|
|
|
|