![]() |
|
|
|||||||
| Home | Forums | Register | Rules & FAQ | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !! |
Other UNIX.COM Threads You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| cat operation | trichyselva | UNIX for Dummies Questions & Answers | 0 | 03-24-2008 02:55 AM |
| Array operation | phamp008 | Shell Programming and Scripting | 3 | 01-18-2008 01:31 AM |
| Help with arithmetic operation | emjayshaikh | Shell Programming and Scripting | 3 | 09-23-2007 08:44 PM |
| RPC failure on yp operation | Remi | SUN Solaris | 1 | 05-22-2007 07:15 AM |
| AIX 4.3 Openssh 3.7.1.0 Operation | ripley | AIX | 2 | 03-07-2005 09:56 AM |
![]() |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
|||
|
Problems with sum operation
i wrote this code..
#!/bin/sh sum=0 for i in `cat numbers.txt |cut -f1` do sum=expr $sum + $i done echo $sum I want to read the numbers in the file numbers.txt. and find sum of them. But this code only writes the numbers(without making sum) that it reads. Help me.. |
| Forum Sponsor | ||
|
|
|
|||
|
Try a bit of awk
burakkilic,
I normally use awk for this kind of problem - it works effectively awk '{tot+=$1} END{print tot}' numbers.txt Alternatively, change your script as shown: #!/bin/sh sum=0 for i in `cat numbers.txt |cut -f1` do sum=`expr $sum + $i` # Add back quotes around the expr done echo $sum Steve |