add new column by calculating percentage used


 
Thread Tools Search this Thread
Operating Systems HP-UX add new column by calculating percentage used
# 1  
Old 08-31-2012
add new column by calculating percentage used

hi..
Code:
 DS_ID | PRIMARYDISKID | SPUID |    GB_USED    |  GB_SIZE
-------+---------------+-------+---------------+------------
     1 |          1027 |  1107 | 82.5849609375 | 356.203125
     2 |          1048 |  1107 | 77.1767578125 | 356.203125


this is file having GB_USED , GB_SIZE two coulmns, i need to calculate the percentage used by (GB_USED /GB_SIZE)*100 and echo this next to GB_SIZE.


Moderator's Comments:
Mod Comment Please use code tags next time for your code and data.

Last edited by zaxxon; 08-31-2012 at 05:20 AM.. Reason: code tags
# 2  
Old 08-31-2012
Code:
$ awk -F\| 'NR==1 {print $0"   "," GB_PERC "} NR==2 {print $0"+---------"} NR>2 {$5=$5" "; $6=sprintf("%8.2f", $4/$5*100); print $0}' OFS=\| infile
 DS_ID | PRIMARYDISKID | SPUID |    GB_USED    |  GB_SIZE   | GB_PERC
-------+---------------+-------+---------------+------------+---------
     1 |          1027 |  1107 | 82.5849609375 | 356.203125 |   23.18
     2 |          1048 |  1107 | 77.1767578125 | 356.203125 |   21.67

# 3  
Old 08-31-2012
Here's an awk solution. You can modify to add column heading if required.

Code:
awk '{FS="|"; OFS="|"; if (NR<=2) {print} else {print $0 " | " $4/$5*100}}' input_file 
DS_ID | PRIMARYDISKID | SPUID |    GB_USED    |  GB_SIZE
-------+---------------+-------+---------------+------------
     1 |          1027 |  1107 | 82.5849609375 | 356.203125 | 23.1848
     2 |          1048 |  1107 | 77.1767578125 | 356.203125 | 21.6665

# 4  
Old 08-31-2012
thnks zaxxon

its wrkng fine .. the output contain this message also
Code:
awk: (FILENAME=out.test FNR=49) fatal: division by zero attempted

can you please expalin what is this ?


Moderator's Comments:
Mod Comment Already sent you a PM, so please use code tags next time for your code and data.

Last edited by zaxxon; 08-31-2012 at 07:20 AM.. Reason: code tags
# 5  
Old 08-31-2012
What does line 49 of your file look like? If it doesn't have 5 fields (or the 5th is not numeric) then you would get this kind of error.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Add all 4 column entries for similar column ids

Hi, I want to write a script which will add the entries in all columns for the same column id. I can do it in excel, but I need to do this for 384 columns which will come down to 96 (384/4). How can I do this iteratively A A A A B B B B C C C C 1 0 1 0 2 1 4 5 3 4 5 6 2 0 0 2 3 5 70 100 1... (7 Replies)
Discussion started by: Diya123
7 Replies

2. Shell Programming and Scripting

Calculating the epoch time from standard time using awk and calculating the duration

Hi All, I have the following time stamp data in 2 columns Date TimeStamp(also with milliseconds) 05/23/2012 08:30:11.250 05/23/2012 08:30:15.500 05/23/2012 08:31.15.500 . . etc From this data I need the following output. 0.00( row1-row1 in seconds) 04.25( row2-row1 in... (5 Replies)
Discussion started by: ks_reddy
5 Replies

3. Shell Programming and Scripting

Calculating average for every Nth line in the Nth column

Is there an awk script that can easily perform the following operation? I have a data file that is in the format of 1944-12,5.6 1945-01,9.8 1945-02,6.7 1945-03,9.3 1945-04,5.9 1945-05,0.7 1945-06,0.0 1945-07,0.0 1945-08,0.0 1945-09,0.0 1945-10,0.2 1945-11,10.5 1945-12,22.3... (3 Replies)
Discussion started by: ncwxpanther
3 Replies

4. Shell Programming and Scripting

to add special tag to a column based on column condition

Hi All, I have following html code <TR><TD>9</TD><TD>AR_TVR_TBS </TD><TD>85000</TD><TD>39938</TD><TD>54212</TD><TD>46</TD></TR> <TR><TD>10</TD><TD>ASCV_SMY_TBS </TD><TD>69880</TD><TD>33316</TD><TD>45698</TD><TD>47</TD></TR> <TR><TD>11</TD><TD>ARC_TBS ... (9 Replies)
Discussion started by: ckwan
9 Replies

5. Shell Programming and Scripting

calculating column summation in a directory of flat files

Hello Guru s I need your kind help to solve my below issue I have a directory of flat files and have to calculate sum of some columns from the flat file . Say for flat file 302 I need the column summation of 2 and 3 rd column For flat file 303 I need the column summation of 5 and... (2 Replies)
Discussion started by: Pratik4891
2 Replies

6. Shell Programming and Scripting

Calculating Swap percentage utilization

Hello All, I am trying to create a script that will give me the processes that consume swap in %. i am using the below line to get it done. virtual=`echo "$virtual/$swp*100"|bc -l|sed -e "s/\(\.\).*/\1/g"` but getting the following output after running it. .039 .110 I want the... (3 Replies)
Discussion started by: ajaincv
3 Replies

7. Shell Programming and Scripting

Need to add letters to a column and add in a new column subtracting from another column

So I have this input 1 10327 rs112750067 T C . PASS DP=65;AF=0.208;CB=BC,NCBI 1 10469 rs117577454 C G . PASS DP=2055;AF=0.020;CB=UM,BC,NCBI 1 10492 rs55998931 C T . PASS DP=231;AF=0.167;CB=BC,NCBI 1 10583 rs58108140 G A ... (3 Replies)
Discussion started by: kellywilliams
3 Replies

8. UNIX for Dummies Questions & Answers

Is it possible to extract rows with the same first column and then calculate its percentage?

A short excerpt of my .txt file looks like: CXRA3Z2J9MQKR B CXRA3Z2J9MQKR A CXRA3Z2J9MQKR C CXRA3Z2J9MQKR B A162JX4ML69UIC C A162JX4ML69UIC A FZ9Z19TI2XOA5 A FZ9Z19TI2XOA5 C FZ9Z19TI2XOA5 B FZ9Z19TI2XOA5 B BRNTTJUB8GXE9 A BRNTTJUB8GXE9 A ... (7 Replies)
Discussion started by: pxalpine
7 Replies

9. UNIX for Dummies Questions & Answers

Calculating the Standard Deviation for a column

Hi all, I want to calculate the standard deviation for a column (happens to be column 3). Does any know of simple awk script to do this? Thanks (1 Reply)
Discussion started by: kylle345
1 Replies

10. Shell Programming and Scripting

How to calculate the percentage for the values in column

Hi, I am having the file which contains the following two columns. 518 _factorial 256 _main 73 _atol 52 ___do_global_ctors 170 ___main 52 ___do_g How can calculate the percentage of each value in the first column ? first need to get the sum of the first column and... (3 Replies)
Discussion started by: saleru_raja
3 Replies
Login or Register to Ask a Question