doing a for loop adding up the results


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting doing a for loop adding up the results
# 1  
Old 12-08-2009
doing a for loop adding up the results

Hi there

If I run a 'swap -l' on my solaris box, i get

swapfile dev swaplo blocks free
/dev/dsk/c1t0d0s1 54,65 8 67119560 65655144
/dev/dsk/c1t0d0s2 54,65 8 33119522 32655122


I wanted to run a for loop adding up the totals of each column 4 , excluding the header then divide the total by 2, so i got as far as

Code:
for n in `swap -l | grep -v swapfile | awk {'print $4'}; do

# Then i drew a blank on how exactly im going to add them all up 

done

can anybody point me in the right direction ?

Any help would be greatly appreciated
# 2  
Old 12-08-2009
Code:
swap -l | /usr/xpg4/bin/awk '/^\//{c+=$4}END{print (c/2)}'

# 3  
Old 12-08-2009
Quote:
Originally Posted by danmero
Code:
swap -l | /usr/xpg4/bin/awk '/^\//{c+=$4}END{print (c/2)}'

Thanks you very much for the reply, i prompmtly went and tried it out (i used nawk as i dont have the awk you specified)


Code:
[root] # swap -l | nawk '/^\//{c+=$4}END{print (c/2)}'
50119560



perfect , thankyou
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Adding results up

Hi There Just created a .sql script and executes fine, it comes back with two lines of results, I was wondering is there a way of adding up the two results to get a round number. I tired wc -l but that didn't work. Many Thanks for your help psql -t -f... (3 Replies)
Discussion started by: simpsa27
3 Replies

2. Shell Programming and Scripting

Df -h results in a loop

Hello everyone, I am doing a check of the disk space using df -h, I want to combine the result in break line; but the result after while/done is empty: # df -h Filesystem Size Used Avail Use% Mounted on rootfs 20G 14G 4.6G 75% / /dev/root 20G 14G 4.6G 75% /... (15 Replies)
Discussion started by: Abu Rayane
15 Replies

3. Shell Programming and Scripting

Narrowing sed Results in While Loop

find $SRC -type f -name *.emlx | while read FILE do if : then sed -n '/From/p' $FILE fi done > $DEST-output.txt The loop above spits out a .txt file with several lines that look like this: From: John Smith <jsmith@company.com> How can I narrow that sed result to spit out the email... (5 Replies)
Discussion started by: sudo
5 Replies

4. UNIX for Dummies Questions & Answers

Adding previous results

Hi, I am trying to use awk to do the following: For a column: 5 3 4 2 7 I want to start with the first entry then add the second to the first, third to second ..etc: 5 8 12 14 21 (5 Replies)
Discussion started by: cosmologist
5 Replies

5. Shell Programming and Scripting

Loop through awk results

I have files structured in stanzas, whose title is '', and the rest couples of 'id: value'. I need to find text within the title and return the whole stanzas that match the title. The following works: awk 'BEGIN{RS="";IGNORECASE=1}/^\/' myfileI would need to count all of the occurences, though,... (7 Replies)
Discussion started by: hermes14
7 Replies

6. Shell Programming and Scripting

Adding grep'd results in a variable

Here is one I am baffled with; I have not used unix for a while and now that I am back it has been fun remembering and I have enjoyed it, for the most past. this is in ksh. I need to search in a file for the line with X1 and cut columns 20-25, put them into a variable, added them (dollar... (3 Replies)
Discussion started by: CougarMutt
3 Replies

7. Shell Programming and Scripting

Adding results of a find to an array

I'm trying to add the paths of all the xml files in certain directories to an array. I want to use the array later in my code. Anyway, for some reason this isn't working. Any help would be appreciated. Path_Counter=0 for result in "find * -name '*.xml'"; do XmlPath="$result" echo... (2 Replies)
Discussion started by: Fly_Moe
2 Replies

8. Shell Programming and Scripting

Concatenate Loop Results

Hi, I have the following situation: Param1Values = AAAA,BBBB Param1=$(echo $Param1Values| tr "," "\n") for x in $Param1 do db2 select X from Y where Z IN ('$x') done Obviously the above will perform the select 'x' amount of times. Is there a way in which i can... (13 Replies)
Discussion started by: RichZR
13 Replies

9. Shell Programming and Scripting

Not able to store the results of perl recursive function when applied under for loop

Hi Perl Gurus , need URGENT HELP PLEASE !!!!! I have one recursive Perl function which takes path of any directory as argument and returns array containing all the sub folders inside it recursively. Now the problem is that it works well if i use it with one time but the problem is that when... (0 Replies)
Discussion started by: anthriksh2000
0 Replies

10. Shell Programming and Scripting

2 CMD results on the same line while rexing in a loop

Folks, I have a 3 problems. In a sh script, I call a server name from a list and rex to a distant machine to get the boot date. for i in `cat list` do (echo "$i|"; /bin/rexsh $i -l bozo -t10 who -b | cut -d" " -f14-16) >>getBootTimes.out sleep 1 done The results are on 2 lines instead... (8 Replies)
Discussion started by: linux_lou
8 Replies
Login or Register to Ask a Question